How do I cancel an Android http request?

16,226

Solution 1

httpclient.getConnectionManager().shutdown();

Solution 2

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.

Solution 3

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.

Share:
16,226
Teddy
Author by

Teddy

I'm a CS student at Middlebury College in Vermont. I keep a blog at www.teddyknox.me.

Updated on June 04, 2022

Comments

  • Teddy
    Teddy almost 2 years

    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?

  • Teddy
    Teddy over 12 years
    This does not interupt the request, do in background finishes.
  • Dave
    Dave over 12 years
    It only attempts to cancel the request, it doesn't guarantee it. Forcefully terminating a thread is generally a bad idea, hence this behaviour.
  • AlanKley
    AlanKley almost 11 years
    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
    AlanKley almost 11 years
    The general solution of calling abort() was not sufficient for me. I followed ayblin's solution and that finally worked for me. stackoverflow.com/questions/7007731/…
  • Jimmy Ilenloa
    Jimmy Ilenloa about 10 years
    I was able to do this and it actually aborts. My approach uses CloseableHttpClient and its related classes. Checkout my answer here: stackoverflow.com/questions/7007731/…
  • user963601
    user963601 about 10 years
    I avoided the android.os.NetworkOnMainThreadException by moving my calls to HttpGet.cancel() onto a background thread. So annoying. :(
  • Adel Nizamuddin
    Adel Nizamuddin about 9 years
    this will shut down the whole client, not the individual request