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 8747569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:22:40+00:00 2026-06-13T12:22:40+00:00

Please refer to the following code, which grabs some information from a PHP script,

  • 0

Please refer to the following code, which grabs some information from a PHP script, and displays it on my phone:

package room.temperature;

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

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.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>();
    }

    @Override
    public void onResume() {
        super.onResume();
        //setValues();

        for (int i = 0; i < 10; i++) {
            setValues();
        }
    }

    public void setValues() {

        InputStream is = null;

        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://mywebsite.com/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());
        }

        System.out.println(result);

        String[] values = result.split("&");

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

    }
}

This code works great when I comment out the for-loop, and just call setValues() once. However, as soon as I introduce the loop, main.xml (or anything on the screen) does not load until the loop is complete. I thought it had something to do with the activity life cycle, but no matter where I have tried putting my code, it simply is not working right. Essentially, I want the values to keep updating every time I call setValues(). A 10 iteration loop is just for testing the concept.

  • 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-13T12:22:41+00:00Added an answer on June 13, 2026 at 12:22 pm

    You need to understand how the UI thread works to understand why you don’t see anything until the loop completes. The UI thread contains a message loop where messages (you can think of them as small blocks of code) are pushed onto and executed serially (1 at a time). When the UI thread runs your code, it will do all that you ask of it but the values on screen will be “posted”, or sent into the message loop to actually be placed on the screen at the next available time. You are not supposed to do long running operations on the UI thread, which is why the Android system has come up with multiple ways of dealing with long running operations. The most common scenario is to use an AsyncTask.

    Http calls should never be executed on the UI thread, in fact this will fail on devices running honeycomb or later (unless you introduce some hacks in your code).

    You should put your http calls in an AsyncTask in the doInBackground method. Then set the onPostExecute method you can make your calls to the UI methods (.setText methods).
    You’ll want to put all the data into some kind of data structure such as an ArrayList in the doInBackground method.

    • 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 of from the Javadoc of Future class: FutureTask<String>
Please refer to the following code which continuously calls a new AsyncTask . The
Please refer to the following code: Filename: myclass.h @interface myclass:NSObject ... @end @interface NSObject(CategoryName)
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 following code snippet. I want to use the std::bind for overloaded
Please refer my previous question for code sample Sockets: sometimes (rarely) packets are lost
Please refer to my code below. When optimization in IAR MSP430 compiler is set

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.