Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8735769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:08:53+00:00 2026-06-13T10:08:53+00:00

Please refer to the following code which continuously calls a new AsyncTask . The

  • 0

Please refer to the following code which continuously calls a new AsyncTask. The purpose of the AsyncTask is to make an HTTP request, and update the string result.

package room.temperature;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class RoomTemperatureActivity extends Activity {

    String result = null;
    StringBuilder sb=null;

    TextView TemperatureText, DateText;
    ArrayList<NameValuePair> nameValuePairs;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TemperatureText = (TextView) findViewById(R.id.temperature); 
        DateText = (TextView) findViewById(R.id.date); 
        nameValuePairs = new ArrayList<NameValuePair>();

        for (int i = 0; i < 10; i++) {          
            RefreshValuesTask task = new RefreshValuesTask();
            task.execute("");
        }
    }

    // The definition of our task class
    private class RefreshValuesTask extends AsyncTask<String, Integer, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... params) {
            InputStream is = null;

            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://mywebsite.com/roomtemp/tempscript.php");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
            }

            catch(Exception e)  {
                Log.e("log_tag", "Error in http connection" + e.toString());
            }

            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                sb = new StringBuilder();
                sb.append(reader.readLine());
                is.close();
                result=sb.toString();
            }

            catch(Exception e)  {
                Log.e("log_tag", "Error converting result " + e.toString());
            }

            return result;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            //System.out.println(result);
            setValues(result);
        }
    }

    public void setValues(String resultValue) {
        System.out.println(resultValue);
        String[] values = resultValue.split("&");

        TemperatureText.setText(values[0]);
        DateText.setText(values[1]);
    }
}

The problem I am experiencing relates to the AsyncTask in some way or the function setValues(), but I am not sure how. Essentially, I want each call to the AsyncTask to run, eventually in an infinite while loop, and update the TextView fields as I have attempted in setValues. I have tried since yesterday after asking a question which led to this code, for reference.

Oh yes, I did try using the AsyncTask get() method, but that didn’t work either as I found out that it is actually a synchronous call, and renders the whole point of AsyncTask useless.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:08:55+00:00Added an answer on June 13, 2026 at 10:08 am

    Use publishProgress(), and onProgressUpdate() methods, to publish progress, while executing some task in doInBackground() method.

    so change your code to following:

    package room.temperature;
    
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.concurrent.ExecutionException;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    
    import android.app.Activity;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.TextView;
    
    public class RoomTemperatureActivity extends Activity {
    
        String result = null;
        StringBuilder sb=null;
    
        TextView TemperatureText, DateText;
        ArrayList<NameValuePair> nameValuePairs;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            TemperatureText = (TextView) findViewById(R.id.temperature); 
            DateText = (TextView) findViewById(R.id.date); 
            nameValuePairs = new ArrayList<NameValuePair>();
    
             RefreshValuesTask task = new RefreshValuesTask();
             task.execute("");
        }
    
        // The definition of our task class
        private class RefreshValuesTask extends AsyncTask<String, Integer, String> {
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }
    
            @Override
            protected String doInBackground(String... params) {
                InputStream is = null;
                for (int i = 0; i < 10; i++) {          
    
                try {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http://mywebsite.com/roomtemp/tempscript.php");
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();
                }
    
                catch(Exception e)  {
                    Log.e("log_tag", "Error in http connection" + e.toString());
                }
    
                try {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                    sb = new StringBuilder();
                    sb.append(reader.readLine());
                    is.close();
                    result=sb.toString();
                    publishProgress(result);
                }
    
                catch(Exception e)  {
                    Log.e("log_tag", "Error converting result " + e.toString());
                 }
                }
                return result;
            }
    
            @Override
            protected void onProgressUpdate(String... values) {
                super.onProgressUpdate(values);
                setValues(values);
            }
    
            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                //System.out.println(result);
                setValues(result);
            }
    
    
        }
    
        public void setValues(String resultValue) {
            System.out.println(resultValue);
            String[] values = resultValue.split("&");
    
            TemperatureText.setText(values[0]);
            DateText.setText(values[1]);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please refer to the following code, which grabs some information from a PHP script,
Please refer to the following code of from the Javadoc of Future class: FutureTask<String>
Please refer to the following code: Filename: myclass.h @interface myclass:NSObject ... @end @interface NSObject(CategoryName)
Please refer the following code snippet. I want to use the std::bind for overloaded
Please refer to the following code: // // CacheObjectManagerImpl.h #import <Foundation/Foundation.h> //#import CacheObject.h @class
I have question please refer the following code to understand the question. (I removed
Please refer to the following java source code : static class SynchronizedList<E> extends SynchronizedCollection<E>
Hi please refer the following HTML code: <div id=content> <p> <font size='2'> <img src=something.jpg
Please refer the fiddle http://jsfiddle.net/HCqsM/5/ Here By clicking the 'click' link for the first
Please refer to the topic http://www.codeproject.com/KB/viewstate/SaveViewState.aspx . The topic demonstrates how you can save

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.