开发者

How do I cancel an Android http request?

开发者 https://www.devze.com 2023-03-29 11:06 出处:网络
开发者_如何学PythonI\'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.

开发者_如何学PythonI'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?


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.


httpclient.getConnectionManager().shutdown();


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号