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.
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
doInBackgroundmethod. Then set theonPostExecutemethod 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.