2

In my app, I use AsyncTask classes to make HTTP connections and download information to display.

Sometimes, the amount of data is substantial and takes some time. If a user and their phone happens to be in a place with slow/poor/spotty reception, there might be HTTP timeouts or maybe they loose connectivity all together, etc...

What is the best way to handle these timeouts gracefully?

I've been expirimenting with creating a Runnable handler that runs XX seconds after starting the AsyncTask. The Runnable would simply test to see if the AsyncTask is still running and if so, assume the data connection is too slow and go ahead and kill the thread and gracefully give an error message about the data connection.

Is this a good approach or is there a more practical method?

Jake Wilson
  • 88,616
  • 93
  • 252
  • 370

1 Answers1

1

It's been a while since I've programmed for Android, but I know that AsyncTask has an update event (to track download percentages and such). You could probably use that to have it check itself.

Stephen
  • 7,994
  • 9
  • 44
  • 73
  • That's only good for a situation where you know how many iterations of something you are doing. In my case, I'm downloading a bunch of content from a webpage. There really isn't a simple way to iterate through that process (except maybe determining the Content size from HTTP headers ahead of time or something along those lines...). – Jake Wilson Nov 21 '11 at 21:01
  • 2
    I can't believe that such a poor anwer got the green mark LOL! – Josh Jun 11 '14 at 07:36