Android-HttpUrlConnection没有关闭。最终导致SocketException

【字号: 日期:2024-02-25浏览:45作者:雯心
如何解决Android-HttpUrlConnection没有关闭。最终导致SocketException?

我终于找到了解决方法。似乎软糖在“保持活动”连接上有问题。我刚刚将Connection =Close添加到我的请求标头中,现在一切正常。在执行netstat时,我看到连接现在正在关闭,并且由于“打开的文件太多”,我不再得到SocketException。

解决方法

我在运行Jellybean(4.1-4.3)的设备中遇到HttpUrlConnection的一些问题,其中连接未关闭,并且执行多次后导致SocketException“打开的文件过多”。

我确实调用了HttpUrlConnection.disconnect()并在finally块中关闭了所有Inputstream,Outputstream,Reader和Writers。

转到adb shell并执行a将netstat显示该应用程序创建的所有连接均处于CLOSE_WAIT状态。

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上运行的其他设备,但没有遇到此问题。

还有另一种方法可以手动关闭连接吗?

相关文章: