gpt4 book ai didi

Android:从互联网下载文件时互联网关闭时如何处理?

转载 作者:行者123 更新时间:2023-11-30 04:03:48 24 4
gpt4 key购买 nike

Android:我正在使用这种方法从互联网上下载文件:

InputStream in = null;
try {
in = connection.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
byte[] buffer = new byte[1024];
int length = 0;
try {
while ((length = in.read(buffer)) != -1) {
try {
fout.write(buffer, 0, length);
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
try {
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}

问题是,当互联网关闭时,read inoutstream 在某些设备上停止读取,但在其他设备上继续读取!如何解决这个问题呢?提前致谢。

编辑:我找到了解决方案,我必须设置连接超时。

最佳答案

建立连接时,将其放入 try-catch 以了解建立连接是否失败。在 catch block 中处理您的问题。我不完全知道你是如何建立连接的,所以无法提供进一步的建议。

关于Android:从互联网下载文件时互联网关闭时如何处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12075496/

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