Android的 - HttpURLConnection的不打烊。最终结果以SocketException [英] Android - HttpUrlConnection is not closing. Eventually results to SocketException

查看:175
本文介绍了Android的 - HttpURLConnection的不打烊。最终结果以SocketException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些问题,运行杰利贝恩(4.1 - 4.3)中的HttpURLConnection中的设备,其中连接不关闭,结果SocketException异常打开的文件过多执行的若干倍。

I am encountering some problems with the HttpUrlConnection in devices running Jellybean (4.1 - 4.3) wherein connections are not closed and results to a SocketException "Too many open files" after executing a number of times.

我叫HttpUrlConnection.disconnect()和我关闭所有的InputStream,OutputStream中,读者和作家finally块。

I do call HttpUrlConnection.disconnect() and am closing all the Inputstream, Outputstream, Reader and Writers in a finally block.

将亚行外壳和执行的netstat 显示所有的应用程序创建的连接被留在CLOSE_WAIT状态。

Going to adb shell and executing a netstat shows all the connections created by the application are left in CLOSE_WAIT state.

InputStream inputStream = httpUrlConnection.getInputStream();

// After calling inputStream.read() then the problem occurs. I think the 
// inputstream doesn't get closed even after calling close in a finally block. 
// The InputStream is a ChunkedInputStream if that helps.

我尝试了2.3.3,4.0.3和4.4上运行的其他设备并没有遇到这个问题。

I have tried other devices running on 2.3.3, 4.0.3 and 4.4 and did not encounter this issue.

有另一种方式,我可以手动关闭连接?

Is there another way that I can manually close the connections?

推荐答案

我终于找到了解决办法。看来,杰利贝恩正在对保持活动连接的问题。我只是说连接=关闭我的请求头,现在一切正常。做一个netstat的,我看到,现在连接被关闭,我不再让SocketException由于打开的文件太多。

I finally found a workaround. It seems that Jellybean is having an issue on "Keep-Alive" connections. I just added Connection=Close to my request header and now all is working. Doing a netstat, I see that the connections are now being closed and I no longer get the SocketException due to "Too many open files".

这篇关于Android的 - HttpURLConnection的不打烊。最终结果以SocketException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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