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();
}


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

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

最佳答案

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

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

10-16 22:05