简体   繁体   English

如何取消Android http请求?

[英]How do I cancel an Android http request?

I'm using AsyncTask to initalize AndroidHttpClient and execute a POST request in doInBackground() . 我正在使用AsyncTask AndroidHttpClient并在doInBackground()执行POST请求。 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() . AsyncTask具有cancel()方法,该方法仅改变的布尔值返回isCancelled()然后等待doInBackground()调用之前完成onCancelled() This means that AsyncTask leaves it up to the doInBackground() method to continuously check whether the task has been cancelled (using isCancelled() ). 这意味着AsyncTask将其doInBackground()方法来连续检查任务是否已被取消(使用isCancelled() )。 If it has been cancelled, doInBackground() should return prematurely prematurely. 如果它已被取消, doInBackground()应该过早地提前返回。 The problem I'm having is that 99% the execution of the worker thread in doInBackground() stops on: 我遇到的问题是99%的doInBackground()中的工作线程的执行停止:

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. 我也在考虑重写AsyncTaskcancel()方法,这可能会更好。

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() . 使用AndroidHttpClient API中止请求的正确方法是在传递给httpClient.execute()的请求对象上调用abort() httpClient.execute() This will cause the execute() call to throw an IOException , and the request's isAborted() call to begin returning true. 这将导致execute()调用抛出IOException ,并且请求的isAborted()调用开始返回true。 The abort() call is defined in the AbortableHttpRequest interface, which basically all of the interesting request classes implement ( HttpGet , HttpPost , etc). abort()调用在AbortableHttpRequest接口中定义,基本上所有有趣的请求类都实现( HttpGetHttpPost等)。

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). AsyncTask.cancel(true)不会取消请求,因为AndroidHttpClient.execute()不会以某种方式阻塞Thread.interrupt()会做什么停止(线程不等待加入另一个线程,等待互斥,或睡觉)。 Thread.interrupt() does NOT stop blocking I/O. Thread.interrupt()不会停止阻塞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.close()httpClient.getConnectionManager().shutdown()不会阻止当前正在执行的请求,但我没有深入研究以试图找出原因。

httpclient.getConnectionManager().shutdown();

Call HttpPost 's or HttpGet 's abort() method. 调用HttpPostHttpGetabort()方法。 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. 如果您正在使用ClientConnectionManager ,则需要设置System.setProperty("http.keepAlive", "false") ,否则后续请求将挂起。 The connection keep alive issue still exists all the way up to at least 4.1.1. 连接保持活动问题仍然存在至少4.1.1。

实际上,可以使用true作为终止线程的参数来调用AsyncTask.cancel(boolean interruptIfRunning)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM