gpt4 book ai didi

android - 互联网连接丢失时不停止读取 Inputstream

转载 作者:太空宇宙 更新时间:2023-11-03 12:10:39 26 4
gpt4 key购买 nike

我正在使用异步任务下载文件。它正常工作,直到我关闭我的 android 的 wifi 连接(没有其他互联网连接),下载对话框仍然没有变化。当我查看日志时,我发现 inputstream 的函数 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() 设置了超时,您应该在连接断开后不久收到 SocketTimeoutException

您的代码是否碰巧可以静默捕获此异常?

关于android - 互联网连接丢失时不停止读取 Inputstream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11838849/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com