14

I'm using AsyncTask to initalize AndroidHttpClient and execute a POST request in doInBackground(). I'd like the user to be able to cancel the request by pressing the back button. AsyncTask has a cancel() method which only changes the boolean return value of isCancelled() and then waits for doInBackground() to finish before calling onCancelled(). This means that AsyncTask leaves it up to the doInBackground() method to continuously check whether the task has been cancelled (using isCancelled()). If it has been cancelled, doInBackground() should return prematurely prematurely. The problem I'm having is that 99% the execution of the worker thread in doInBackground() stops on:

HttpResponse response = httpClient.execute(request[0]);

because this synchronous function call encapsulates the network aspects of the request. How can I cancel the request midway through?

I'm considering trying to change the timeout time during the request, but this seems thread unsafe.

I'm also considering overriding AsyncTask's cancel() method, which may work better.

Any suggestions?

erakitin
  • 11,437
  • 5
  • 44
  • 49
Teddy
  • 1,996
  • 5
  • 21
  • 33

4 Answers4

19

The correct way to abort a request with the AndroidHttpClient API is to call abort() on the request object that you pass into httpClient.execute(). This will cause the execute() call to throw an IOException, and the request's isAborted() call to begin returning true. The abort() call is defined in the AbortableHttpRequest interface, which basically all of the interesting request classes implement (HttpGet, HttpPost, etc).

AsyncTask.cancel(true) doesn't cancel the request, because AndroidHttpClient.execute() doesn't block in a way that Thread.interrupt() will do anything to stop (the thread is not waiting to join another thread, waiting on a mutex, or sleeping). Thread.interrupt() does NOT stop blocking I/O.

In my testing, calling httpClient.close() or httpClient.getConnectionManager().shutdown() does nothing to stop the currently-executing request, but I didn't research deeply to try and find out why.

erakitin
  • 11,437
  • 5
  • 44
  • 49
Jeremy Penner
  • 361
  • 2
  • 4
  • Occasionally, I will get an android.os.NetworkOnMainThreadException when doing this. My HTTP request is already running in an AsyncTask, should I not be accessing the request object in the UI thread to call abort on it? – AlanKley Jun 21 '13 at 04:45
  • The general solution of calling abort() was not sufficient for me. I followed ayblin's solution and that finally worked for me. http://stackoverflow.com/questions/7007731/how-to-cancel-defaulhttpclient-execution-process-in-multithread – AlanKley Jun 21 '13 at 18:53
  • 1
    I avoided the `android.os.NetworkOnMainThreadException` by moving my calls to `HttpGet.cancel()` onto a background thread. So annoying. :( – Heath Borders Apr 29 '14 at 16:38
11
httpclient.getConnectionManager().shutdown();
duggu
  • 37,851
  • 12
  • 116
  • 113
Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
2

Call HttpPost's or HttpGet's abort() method. You can abort the request which throws a (I forget which particular) exception. If you're using a ClientConnectionManager you'll want to set System.setProperty("http.keepAlive", "false"), otherwise subsequent requests will just hang. The connection keep alive issue still exists all the way up to at least 4.1.1.

erakitin
  • 11,437
  • 5
  • 44
  • 49
Noah Seidman
  • 4,359
  • 5
  • 26
  • 28
-1

Actually AsyncTask.cancel(boolean interruptIfRunning) can be called using true as the parameter to terminate the thread.

Dave
  • 6,064
  • 4
  • 31
  • 38
  • 1
    This does not interupt the request, do in background finishes. – Teddy Aug 21 '11 at 20:30
  • 1
    It only attempts to cancel the request, it doesn't guarantee it. Forcefully terminating a thread is generally a bad idea, hence this behaviour. – Dave Aug 25 '11 at 13:38
  • I was able to do this and it actually aborts. My approach uses **CloseableHttpClient** and its related classes. Checkout my answer here: http://stackoverflow.com/questions/7007731/how-to-cancel-defaulhttpclient-execution-process-in-multithread/21915242#21915242 – Jimmy Ilenloa Feb 21 '14 at 11:12