如何取消 Android http 请求?
我使用 AsyncTask
初始化 AndroidHttpClient
并在 doInBackground()
中执行 POST 请求。我希望用户能够通过按后退按钮取消请求。 AsyncTask
有一个 cancel()
方法,该方法仅更改 isCancelled()
的布尔返回值,然后等待 doInBackground()< /code> 在调用
onCancelled()
之前完成。这意味着 AsyncTask
将其交给 doInBackground()
方法来连续检查任务是否已被取消(使用 isCancelled()
)。如果已经被取消,doInBackground()
应该提前返回。我遇到的问题是 doInBackground()
中工作线程的执行 99% 停止于:
HttpResponse response = httpClient.execute(request[0]);
因为此同步函数调用封装了请求的网络方面。如何中途取消请求?
我正在考虑尝试更改请求期间的超时时间,但这似乎线程不安全。
我还在考虑重写 AsyncTask
的 cancel()
方法,这可能会更好。
有什么建议吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 AndroidHttpClient API 中止请求的正确方法是对传入
httpClient.execute()
的请求对象调用abort()
。这将导致execute()
调用抛出IOException
,并且请求的isAborted()
调用开始返回 true。abort()
调用在AbortableHttpRequest
接口中定义,基本上所有有趣的请求类都实现了 (HttpGet
,HttpPost< /代码>等)。
AsyncTask.cancel(true)
不会取消请求,因为AndroidHttpClient.execute()
不会以Thread.interrupt()< 的方式阻塞/code> 将执行任何操作来停止(线程不会等待加入另一个线程、等待互斥体或休眠)。
Thread.interrupt()
不会停止阻塞 I/O。在我的测试中,调用
httpClient.close()
或httpClient.getConnectionManager().shutdown()
不会阻止当前正在执行的请求,但我没有研究深入尝试找出原因。The correct way to abort a request with the AndroidHttpClient API is to call
abort()
on the request object that you pass intohttpClient.execute()
. This will cause theexecute()
call to throw anIOException
, and the request'sisAborted()
call to begin returning true. Theabort()
call is defined in theAbortableHttpRequest
interface, which basically all of the interesting request classes implement (HttpGet
,HttpPost
, etc).AsyncTask.cancel(true)
doesn't cancel the request, becauseAndroidHttpClient.execute()
doesn't block in a way thatThread.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()
orhttpClient.getConnectionManager().shutdown()
does nothing to stop the currently-executing request, but I didn't research deeply to try and find out why.调用
HttpPost
或HttpGet
的abort()
方法。您可以中止引发(我忘记了哪个特定)异常的请求。如果您使用的是ClientConnectionManager
,您需要设置System.setProperty("http.keepAlive", "false")
,否则后续请求将挂起。连接保持活动问题至少在 4.1.1 之前仍然存在。Call
HttpPost
's orHttpGet
'sabort()
method. You can abort the request which throws a (I forget which particular) exception. If you're using aClientConnectionManager
you'll want to setSystem.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.实际上可以使用
true
作为参数来调用AsyncTask.cancel(booleaninterruptIfRunning)
来终止线程。Actually
AsyncTask.cancel(boolean interruptIfRunning)
can be called usingtrue
as the parameter to terminate the thread.