How to update ui from asynctask

android, android-asynctask, android-handler, android-json

Solution

You have three protected methods in an AsyncTask that can interact with the UI.

- `onPreExecute()`

- runs before `doInBackground()`

- `onPostExecute()`

- runs after `doInBackground()` completes

- `onProgressUpdate()`

- this only runs when `doInBackground()` calls it with `publishProgress()`

If in your case the Task runs for a lot longer than the 30 seconds you want to refresh you would want to make use of `onProgressUpdate()` and `publishProgress()`. Otherwise `onPostExecute()` should do the trick.

See the official documentation for how to implement it.

Problem

I've seen many examples of how to do this, but I can't figure how to implement it in my code. I am using this code. I have updated the url, so it will receive a json with dynamic data. What I'm trying to do is to automatically update the list every 30 secs with this code. ``` Handler handler = new Handler(); Runnable refresh = new Runnable() { public void run() { new GetContacts().execute(); handler.postDelayed(refresh, 30000); } }; ``` It refreshes, and calls the url and gets the data, but the UI does not get updated. Thanks for any hints to point me in the right direction. http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

Original source