我使用异步任务来下载文件。它正常工作,直到我关闭我的 Android 的 wifi 连接(没有其他互联网连接),下载对话框仍然存在并且没有任何变化。当我检查日志时,我发现输入流的函数 read() 不停顿。那么如何检查这个案例呢?这是我的代码:
URL url = new URL(this.url);
URLConnection connection = url.openConnection();
connection.setReadTimeout(1000);
connection.connect();
// this will be useful so that you can show a typical 0-100% progress bar
int fileLength = connection.getContentLength();
fileName = "temp.zip";
// download the file
InputStream input = new BufferedInputStream(connection.getInputStream());
OutputStream output = new FileOutputStream(path+fileName);
byte buffer[] = new byte[1024000];
long total = 0;
int count;
Log.v("test download:","download in background");
while (((count = input.read(buffer)) != -1)) {
Log.v("test download:","read:"+count);
total += count;
publishProgress((int) (total * 100 / fileLength - 1));
output.write(buffer, 0, count);
}
由于您已经通过调用设置了超时setReadTimeout() http://developer.android.com/reference/java/net/URLConnection.html#setReadTimeout(int),你应该得到一个SocketTimeoutException
连接断开后不久。
您的代码是否碰巧以静默方式捕获了此异常?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)