2

I want to use AsyncTask to perform login with a server, and show a progress dialog before the connection ends.

Since there may not be response for the request, I need to set the timeout value for the AsyncTask. I found that when I simply use .execute(), the program works fine but no timeout function is implemented. When I use .get(1000, TimeUnit.MILLISECONDS), the program just halts for 1 second and no progress dialog is shown.

Any one can tell me whether the task is executed when .get(1000, TimeUnit.MILLISECONDS) is called? If yes, why there's no sign of execution; and if not, how can I implement this timeout function of the AsyncTask?

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
user1731839
  • 125
  • 2
  • 9
  • possible duplicate of [ProgressDialog not shown when AsyncTask.get() called](http://stackoverflow.com/questions/9019249/progressdialog-not-shown-when-asynctask-get-called) – Anirudh Ramanathan Oct 30 '12 at 16:10
  • use `onPostExecute()`. As mentioned in the above. – Anirudh Ramanathan Oct 30 '12 at 16:11
  • 3
    Don't use the `get(...)` methods of `AsyncTask`. They are completely pointless IMO as they effectively turn the asynchronous background execution of an `AsyncTask` into a synchronous operation. Basically it will block the main (UI) thread preventing any UI updates and makes the whole idea of using `AsyncTask` redundant. – Squonk Oct 30 '12 at 16:29
  • I already used onPostExecute()... – user1731839 Oct 30 '12 at 16:33
  • possible duplicate of [AsyncTask block UI threat and show progressbar with delay](http://stackoverflow.com/questions/5583137/asynctask-block-ui-threat-and-show-progressbar-with-delay) – Peter O. Jan 10 '13 at 04:16

4 Answers4

3

The AsyncTask.get(), if in the main thread (AKA. UI thread) will block execution.

You probably need call it in a separate thread.

Edit

Vogella made a very great article about this: AndroidPerformance: Android Threads, Handlers And AsyncTask

Take the code from here, I did and I assure you it works great without blocking the main UI thread.

shkschneider
  • 17,833
  • 13
  • 59
  • 112
0

I think you need to set the timeout interval on HttpUrlConnection object that would be better option to hand this situation.

Techie Manoj
  • 432
  • 3
  • 14
0

If you use AndroidHttpClient it has nice preset connection timeouts.

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
0

From the documentation, the get(long timeout, TimeUnit unit) method will wait for the duration specified, then attempt to cancel the task. I think that all this will do is call cancel() on your AsyncTask, rather than performing any kind of timeout on your connection. If you are performing some kind of long download you can check isCancelled in your loop. However if you are just trying to give some kind of connection timeout then get is not the way to do it.

Please also note that get blocks the main thread until it returns. This is true of both the "timeout" version and the base version of the method. As such it is not an asynchronous operation.

To get your connection timeout, you need to perform this on the actual connection that you create, not on the task itself. There are ways of doing this for both an HttpUrlConnection and the HttpClient.

dave.c
  • 10,910
  • 5
  • 39
  • 62