如何取消 Android http 请求?

发布于 2024-11-30 13:50:55 字数 769 浏览 0 评论 0原文

我使用 AsyncTask 初始化 AndroidHttpClient 并在 doInBackground() 中执行 POST 请求。我希望用户能够通过按后退按钮取消请求。 AsyncTask 有一个 cancel() 方法,该方法仅更改 isCancelled() 的布尔返回值,然后等待 doInBackground()< /code> 在调用 onCancelled() 之前完成。这意味着 AsyncTask 将其交给 doInBackground() 方法来连续检查任务是否已被取消(使用 isCancelled())。如果已经被取消,doInBackground()应该提前返回。我遇到的问题是 doInBackground() 中工作线程的执行 99% 停止于:

HttpResponse response = httpClient.execute(request[0]);

因为此同步函数调用封装了请求的网络方面。如何中途取消请求?

我正在考虑尝试更改请求期间的超时时间,但这似乎线程不安全。

我还在考虑重写 AsyncTaskcancel() 方法,这可能会更好。

有什么建议吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烂柯人 2024-12-07 13:50:55

使用 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 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.

愁以何悠 2024-12-07 13:50:55
httpclient.getConnectionManager().shutdown();
httpclient.getConnectionManager().shutdown();
浅黛梨妆こ 2024-12-07 13:50:55

调用 HttpPostHttpGetabort() 方法。您可以中止引发(我忘记了哪个特定)异常的请求。如果您使用的是 ClientConnectionManager,您需要设置 System.setProperty("http.keepAlive", "false"),否则后续请求将挂起。连接保持活动问题至少在 4.1.1 之前仍然存在。

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.

雪若未夕 2024-12-07 13:50:55

实际上可以使用true作为参数来调用AsyncTask.cancel(booleaninterruptIfRunning)来终止线程。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文