Android的奇巧的HttpURLConnection断开的AsyncTask [英] Android KitKat HttpURLConnection disconnect AsyncTask

查看:261
本文介绍了Android的奇巧的HttpURLConnection断开的AsyncTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我使用的是的HttpURLConnection 中的AsyncTask 。如果文件需要太长的时间来下载的话,我想取消它;而我做到这一点,关闭流并调用<一href="http://developer.android.com/reference/java/net/HttpURLConnection.html#disconnect%28%29">disconnect()在的HttpURLConnection 对象。在code有多年在Android中准确无误地运行。然而,现在奇巧已经出来了,问题已经出现了。具体而言,断开连接()调用自身需要几秒钟或更长时间才能完成。在pre-奇巧设备,花了一毫秒以下。什么是真正奇怪的是,当我执行断开呼叫一个单独的线程,那么它是非常快了。因此,它必须是一个问题,称这是在的AsyncTask doInBackground 方法。有一点要注意的是,我的的AsyncTask 确实有一个活套。prepare()通话

In my application, I download a file using an HttpURLConnection in an AsyncTask. If the file takes too long to download, then I want cancel it; and I do this by closing the stream and calling disconnect() on the HttpURLConnection object. The code has worked flawlessly for years in Android. However, now that KitKat is out, issues have popped up. Specifically, the disconnect() call itself takes several seconds or more to complete. In pre-KitKat devices, it took a millisecond or less. What is really strange is that when I perform the disconnect call on a separate thread, then it is really fast again. So it has to be an issue with calling it in the AsyncTask's doInBackground method. One thing to note is that my AsyncTask does have a Looper.prepare() call.

有谁知道奇巧和其他的Andr​​oid版本之间的区别是什么?我梳理通过更改列表,并没有看到任何与此相关的问题。

Does anyone know what the difference between KitKat and other Android versions is? I combed through the change lists and did not see anything related to this issue.

推荐答案

这可能与持久连接< /一>,并尝试HttpURLConnection类的关闭连接或InputStream的时候重新使用相同的HTTP连接,因此,它试图通过阅读他们,并丢弃到消耗所有剩余的数据。然后同样的HTTP连接已准备好下一个命令。

This may be related to persistent connections, and attempt of HttpURLConnection to reuse same HTTP connection, thus when closing connection or InputStream, it tries to consume all remaining data by reading them and discarding. Then same HTTP connection is ready for next command.

似乎是在奇巧一些新的实现,使事情变得不同,会导致问题。

There seems to be some new implementation in KitKat that makes things different and causes problems.

我尝试视频数据流通过HTTP,偶尔寻求导致关闭连接,并发起新的。当然,我并不需要视频的抓取流器将结束于这种情况。

I try to stream video data over HTTP, with occasional seeking which results in closing connection and initiating new one. Certainly I don't need fetching of video stream up to end in this case.

我试着打电话

HttpURLConnection con;
con.setRequestProperty("Connection", "close");

但它并没有区别。

but it didn't make difference.

不过,我尝试使用相同的code用的 DefaultHttpClient 并调用

However, I tried using same code with DefaultHttpClient and calling

HttpGet get = new HttpGet(uri);
get.addHeader("Connection", "close");

那么最终收盘连接的InputStream速度快,这是我证明通过调试步入ConnectionReuseStrategy.keepAlive,看到该服务器返回连接:关闭和DefaultHttpClient没有尝试重用连接,并读取,直到结束。

then finally closing InputStream of connection is fast, which I proved in debugger by stepping into ConnectionReuseStrategy.keepAlive and seeing that server returned Connection:close and DefaultHttpClient didn't try to reuse connection and reading till end.

所以,你可以使用DefaultHttpClient或找到一种方法,使HttpURLConnection类,只是密切的联系,而不读书休息。

So you can use DefaultHttpClient or find a way to make HttpURLConnection to just close connection without reading rest.

这篇关于Android的奇巧的HttpURLConnection断开的AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆