How do I cancel an Android http request?
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.
Teddy
I'm a CS student at Middlebury College in Vermont. I keep a blog at www.teddyknox.me.
Updated on June 04, 2022Comments
-
Teddy almost 2 years
I'm using
AsyncTask
to initalizeAndroidHttpClient
and execute a POST request indoInBackground()
. I'd like the user to be able to cancel the request by pressing the back button.AsyncTask
has acancel()
method which only changes the boolean return value ofisCancelled()
and then waits fordoInBackground()
to finish before callingonCancelled()
. This means thatAsyncTask
leaves it up to thedoInBackground()
method to continuously check whether the task has been cancelled (usingisCancelled()
). If it has been cancelled,doInBackground()
should return prematurely prematurely. The problem I'm having is that 99% the execution of the worker thread indoInBackground()
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
'scancel()
method, which may work better.Any suggestions?
-
Teddy over 12 yearsThis does not interupt the request, do in background finishes.
-
Dave over 12 yearsIt only attempts to cancel the request, it doesn't guarantee it. Forcefully terminating a thread is generally a bad idea, hence this behaviour.
-
AlanKley almost 11 yearsOccasionally, 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 almost 11 yearsThe 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 about 10 yearsI 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 about 10 yearsI avoided the
android.os.NetworkOnMainThreadException
by moving my calls toHttpGet.cancel()
onto a background thread. So annoying. :( -
Adel Nizamuddin about 9 yearsthis will shut down the whole client, not the individual request