【问题标题】:Android: how to handle when the internet is turned off while downloading a file from internet?Android:从互联网下载文件时关闭互联网如何处理?
【发布时间】:2012-08-22 14:35:27
【问题描述】:

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 在某些设备上停止读取,但在其他设备上继续读取!如何解决这个问题呢?提前致谢。

编辑: 我找到了解决办法,我必须设置一个连接的超时时间。

【问题讨论】:

    标签: android download inputstream


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-19
      • 2013-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-03
      相关资源
      最近更新 更多