2012-08-22 153 views
1

安卓:我從互聯網上使用此方法下載文件: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(); 
} 

的問題是,當互聯網被關閉,讀inoutstream停止一些設備上閱讀,但它繼續閱讀上的其他設備!如何解決這個問題呢?提前致謝。

編輯: 我找到了解決方案,我必須設置一個連接的超時。

回答

1

建立連接時,將其放入try-catch以確定建立連接是否失敗。在catch塊中處理您的問題。我不完全知道你是如何建立連接的,所以無法進一步提出建議。