我使用下面的代碼來下載android.code中的zip文件,工作正常,但有時下載失敗並拋出套接字異常。特別是當互聯網連接速度慢(我猜)。我也發佈了logcat錯誤消息的屏幕截圖。從android下載文件時出現套接字異常
int count;
URL url = new URL(URL);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
//Log.e("ANDRO_ASYNC", "Lenght of file: " + "="+lenghtOfFile);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(StorezipFileLocation);
byte data[] = new byte[lenghtOfFile];
long total = 0;
while ((count = input.read(data)) != -1) {
total += c![enter image description here][1]ount;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
我只從logcat得到這個錯誤 – Ruban
如果執行「close()」行時發生錯誤,那麼你的下載應該完成。你有沒有嘗試過抓住免疫?否則,你知道錯誤發生在哪裏嗎?在等待數據時? –