【发布时间】:2017-01-29 12:21:22
【问题描述】:
我有一个函数可以获取服务器上文件的大小。我认识到关闭连接可能需要很长时间,有时需要 10 秒或更长时间。 现在我遇到了这种情况,在 Android 模拟器中它永远挂起,但在真实设备上启动同一个应用程序却正常运行。
有人可以解释这种行为还是有更好的方法来关闭连接?
public static int getFileSizeFromURL(String sUrl) {
URL url;
URLConnection conn;
int size=0;
try {
url = new URL(sUrl);
conn = url.openConnection();
size = conn.getContentLength();
if(size < 0){
} else {
conn.getInputStream().close(); <----- hangs here in Simulator.
}
}
catch(Exception e) {
e.printStackTrace();
}
return size;
}
【问题讨论】:
标签: android httpurlconnection freeze