在android中取消Http连接

发布于 2024-12-02 21:58:38 字数 577 浏览 1 评论 0原文

我正在使用 org.apache.http ,我有这个代码:

DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);

HttpResponse resp = client.execute(get);
HttpEntity entity = resp.getEntity();

InputStream input = entity.getContent();

...
//Read the bytes from input stream

这是我用来通过 Http 下载文件的代码,我想取消连接(可能是用户选择的)什么是关闭连接的优雅方式。我找到了两种方法,都取消下载。

  1. 关闭inputsteram,input.close();这会导致 IOException。
  2. 中止 HttpGet 对象时,get.abort() 会导致 SocketException。

我有 try catch,所以没有错误,但没有抛出异常, 有没有办法取消或中止连接?

正确的做法是什么?

I am using org.apache.http and I've this code:

DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);

HttpResponse resp = client.execute(get);
HttpEntity entity = resp.getEntity();

InputStream input = entity.getContent();

...
//Read the bytes from input stream

This is the code I am using to download files over Http, I want to cancel the connection(may be user chooses to) What is the graceful way to close the connection. I found 2 ways, Both cancels the download.

  1. Closing inputsteram, input.close(); which causes IOException.
  2. Aborting HttpGet object, get.abort() causes SocketException.

I have try catch, so no erros, but without throwing exception,
is there a way to cancel or abort the connection?

What is the right way to go about it ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

束缚m 2024-12-09 21:58:38

正确的方法是将 FIN 值发送到服务器端。

然而在android中你没有选择参与这个级别,所以你可以使用C自行实现,或者使用你在问题中提到的方法之一。

The proper way doing this is sending FIN value to the server side.

How ever in android you do not have the option to be involved in this level, so you can implement by your self using C, or use one of the methods you mention in your question.

沉睡月亮 2024-12-09 21:58:38

在我看来,使用 HttpUriRequest#about 是正确的方法。这将导致底层连接立即终止并将其从连接池中逐出。较新版本的 HttpClient(4.2 及更高版本)会拦截由于用户过早终止请求而导致的 SocketException。问题是 Google 发布了一个基于极其过时版本(pre-beta1)的 HttpClient 分支。如果您不能或不愿意使用较新版本的 HttpClient,您唯一的选择是在代码中捕获并丢弃 SocketException。

Using HttpUriRequest#about is the right way in my opinion. This will cause immediate termination of the underlying connection and its eviction from the connection pool. Newer versions of HttpClient (4.2 and newer) intercept SocketExceptions caused by premature request termination by the user. The problem is that Google ships a fork of HttpClient based on an extremely outdated version (pre-beta1). If you are not able or willing to use a newer version of HttpClient your only option is to catch and discard SocketException in your code.

虐人心 2024-12-09 21:58:38

使用此

client.getConnectionManager().closeExpiredConnections();
client.getConnectionManager().shutdown();

现在您可以决定在代码中的何处编写这两行。它将使用您创建的 DefaultHttpClient 对象关闭连接。

让我知道这是否对您有帮助。

Use this

client.getConnectionManager().closeExpiredConnections();
client.getConnectionManager().shutdown();

Now you can decide where would you like to write these 2 lines in code.. It will close the connection using the DefaultHttpClient object that you created.

Let me know if this helps you.

池予 2024-12-09 21:58:38

当你想中断连接时尝试取消任务:
task.cancel(true);
这将取消任务和其中运行的线程。
检查一下以供参考:
public Final boolean cancel (boolean mayInterruptIfRunning)

自:API 级别 3
尝试取消此任务的执行。如果任务已完成、已取消或由于某些其他原因无法取消,则此尝试将失败。如果成功,并且调用取消时该任务尚未启动,则该任务永远不应运行。如果任务已经启动,则 mayInterruptIfRunning 参数确定是否应中断执行此任务的线程以尝试停止该任务。

调用此方法将导致在 doInBackground(Object[]) 返回后在 UI 线程上调用 onCancelled(Object)。调用此方法可保证永远不会调用 onPostExecute(Object)。调用此方法后,您应该定期从 doInBackground(Object[]) 检查 isCancelled() 返回的值,以尽早完成任务。

参数
mayInterruptIfRunning 如果执行此任务的线程应该被中断,则为 true;否则,允许完成正在进行的任务。
退货
如果任务无法取消,则为 false,通常是因为它已经正常完成;否则为真

Try to cancel the task when you want to interrupt the connection:
task.cancel(true);
This will cancel the task and the threads running in it.
Check this for reference:
public final boolean cancel (boolean mayInterruptIfRunning)

Since: API Level 3
Attempts to cancel execution of this task. This attempt will fail if the task has already completed, already been cancelled, or could not be cancelled for some other reason. If successful, and this task has not started when cancel is called, this task should never run. If the task has already started, then the mayInterruptIfRunning parameter determines whether the thread executing this task should be interrupted in an attempt to stop the task.

Calling this method will result in onCancelled(Object) being invoked on the UI thread after doInBackground(Object[]) returns. Calling this method guarantees that onPostExecute(Object) is never invoked. After invoking this method, you should check the value returned by isCancelled() periodically from doInBackground(Object[]) to finish the task as early as possible.

Parameters
mayInterruptIfRunning true if the thread executing this task should be interrupted; otherwise, in-progress tasks are allowed to complete.
Returns
false if the task could not be cancelled, typically because it has already completed normally; true otherwise

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