6
在我的Android程序中,我有一些下載文件的代碼。這工作正常,但由於在手機上,您可以隨時斷開連接,我需要改變它重新連接,並在中途通過和有人呼叫/丟失信元接收等時恢復下載。我無法弄清楚如何檢測InputStream已停止工作。請參見下面的代碼:Android InputStream互聯網斷開連接
InputStream in = c.getInputStream();
byte[] buffer = new byte[8024];
int len1 = 0;
while ((len1 = in.read(buffer)) > 0) {
Log("-"+len1+"- Downloaded.");
f.write(buffer,0, len1);
Thread.sleep(50);
}
當我失去互聯網連接,我的日誌顯示:
Log: -8024- Downloaded.
Log: -8024- Downloaded.
Log: -8024- Downloaded.
Log: -8024- Downloaded.
Log: -6024- Downloaded. (some lower number)
然後我的程序只是掛在一段時間((LEN1 =等等。我需要做出這等當網絡斷開連接我等待網絡再次連接,然後恢復下載
嗨, 謝謝,但我有一個HTTPURLconnection - 你怎麼從中得到SocketChannel?謝謝, – 2009-02-26 02:57:21