My basic question is how do you update the GUI with AsyncTask. I am setting a String in onPostExecute that the GUI thread references. Using logging, I can see the String getting set in the onPostExecute method, but it never gets set on my GUI under my onClickListener to update the GUI. Any help is appreciated.
Main Program:
public class Main extends Activity {
/** Called when the activity is first created. */ Arduino.ToAndroid.Temperature.GetJSON jsonHttpClass = new Arduino.ToAndroid.Temperature.GetJSON(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); new GetJSON().execute(url_to_Http); } View.OnClickListener temperatureListener = new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub // buttonTemperature = json.getTemp(); tempView.setText(jsonHttpClass.value); Log.i("ROSS LOG", "Button Clicked"); } }; }
Async Task:
class GetJSON extends AsyncTask {
public String value; @Override protected String doInBackground(String... url) { String result = this.getHttpJson(url[0]); return result; } @Override protected void onPostExecute(String result) { value = new String(result); Log.i("ROSS LOG", value); }}
In
onCreate(), you should be using the handle for the already created object of theAsyncTaskand not create a new object.Use
instead of