tags:

views:

62

answers:

1

Hi ,

I just want to check if I understood well the way asynchronous Http request work on Android. Suppose I make such a request and set a ResponseHandler<String> responseHandler to handle the response. By doing this is it possible to have the UI thread blocked waiting for the response ? The implication being that the code in the function:

public String handleResponse(HttpResponse response)

is also executed on the UI thread or is there silently spawned a thread that waits for the response and calls the handleResponse(HttpResponse response) function when the response arrives ?

A: 

By doing this is it possible to have the UI thread blocked waiting for the response ?

Calling execute() with or without a ResponseHandler will block that thread until the HTTP request has been processed. If you call execute() on the main application thread, you will block the UI. This is not a good thing -- use AsyncTask to execute() on a separate thread, then use your onPostExecute() implementation to update your UI with the results of the request.

CommonsWare
Could you provide more information about "onPostExecute()" function ?
rantravee
When you implement an `AsyncTask`, you can override `onPostExecute()`. This is called on the main application thread, after the background work from `doInBackground()` is completed. Here is an example project from one of my books that shows using HttpClient with `AsyncTask`: http://github.com/commonsguy/cw-android/tree/master/Service/WeatherPlus/
CommonsWare
Thanks a bunch !
rantravee