问题描述
我正在使用asynctask下载文件.直到我关闭我的android的wifi连接(没有其他互联网连接),它仍然可以正常工作,下载对话框仍然没有任何变化.当我通过日志检查时,我发现inputstream的read()函数是不间断的.那么如何检查这种情况呢?这是我的代码:
I'a using a asynctask to download file. It works normally until i turn off wifi connection (there are no other internet connection) of my android, download dialog still and no changes. When i check by log, i discover that function read() of inputstream is non stop. So how to check this case? here is my code:
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()
,连接断开后不久,您应该得到一个SocketTimeoutException
.
Since you already set a timeout by calling setReadTimeout()
, you should get a SocketTimeoutException
shortly after the connection dropped.
您的代码是否碰巧会悄无声息地捕获此异常?
Do your code happen to maybe capture this exception silently?
这篇关于互联网连接断开时,Inputstream的读取不会停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!