您好,登錄后才能下訂單哦!
下載過程中的錯誤處理邏輯是:
1. 在DownThread的run方法發現異常,調用sendMessage(int type, int data)創建消息添加到隊列中。
@Override public void run() { InputStream is = null; HttpURLConnection conn = null; try { file.seek(start); URL connUrl = new URL(url); conn = (HttpURLConnection) connUrl.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(DownTask.connectTimeout); conn.setRequestProperty("Range", "bytes=" + start + "-" + end); int responseCode = conn.getResponseCode(); Logger.d(TAG, "下載資源的字節范圍:" + start + "-" + end); if (start > end) { sendMessage(DownMessage.MSG_SUCCESS, 0); return; } if (responseCode == HttpURLConnection.HTTP_PARTIAL || responseCode == HttpURLConnection.HTTP_OK) { is = new BufferedInputStream(conn.getInputStream(), 8192); byte[] buffer = new byte[204800];// 20K緩存 int len = 0; while ((len = is.read(buffer)) != -1 && running) { file.write(buffer, 0, len); sendMessage(DownMessage.MSG_UPDATE, len); } sendMessage(DownMessage.MSG_SUCCESS, 0); } else { sendMessage(DownMessage.MSG_ERROR, 10); } } catch (IOException ex) { try { sendMessage(DownMessage.MSG_ERROR, 0); } catch (InterruptedException e) { e.printStackTrace(); } ex.printStackTrace(); } catch (InterruptedException ex) { ex.printStackTrace(); } finally { try { sendMessage(DownMessage.MSG_STOP, 0); } catch (InterruptedException e1) { } if (file != null) { try { file.close(); } catch (IOException e) { } } file = null; if (is != null) { try { is.close(); } catch (IOException e) { } } is = null; if (conn != null) conn.disconnect(); conn = null; release(); } }
2. 在DownTask的run方法之while循環中取出隊列中的消息并且進行處理。不同類別的消息分別進行不同處理,消息類別有:MSG_SUCCESS, MSG_ERROR, MSG_UPDATE, MSG_STOP等。
DownMessage msg; running = true; while (running) { msg = queue.take(); switch (msg.type) { case DownMessage.MSG_SUCCESS: running = false; if (downListener != null) downListener.onDownFinish(); break; case DownMessage.MSG_UPDATE: if (downListener != null) { downListener.onDownUpdate(msg.data); } break; case DownMessage.MSG_ERROR: running = false; if (downThread != null) { downThread.canleDown(); } notifyDownError(ERROR_UNKNOWN); break; case DownMessage.MSG_STOP: running = false; if (downListener != null) { downListener.onDownPause(); } break; } recyle(msg);
downListener.onDownError因大量使用,所以在提取出來,放在notifyDownError方法中。
/** * 下載出錯 * * @param type */ private void notifyDownError(int type) { if (downListener != null) downListener.onDownError(type); }
3. 在DownService中實現OnDownListener接口,在接口方法中調用句柄Handler對象來處理不同類別的消息,處理操作包括更新數據庫表,展現Toast提示等。
在啟動一個下載任務時,實現OnDownListener接口。
/** * 啟動一個下載任務。 * * @param down * @param downTask */ public void launchDownloadTask(final Down down, DownTask downTask) { downListener = new OnDownListener() { @Override public void onDownUpdate(int len) { downHandler.obtainMessage(UPDATE_DOWN, len, 0, down).sendToTarget(); } @Override public void onDownStart(int total) { if (down.getLength() < 1) { down.setLength(total); down.setLengthStr(Formatter.formatFileSize(DownService.this, total)); } } @Override public void onDownFinish() { downHandler.obtainMessage(DOWN_FINISH, down).sendToTarget(); } @Override public void onDownError(int type) { handler.obtainMessage(DOWN_FAIL, type, 0, down).sendToTarget(); } @Override public void onDownPause() { downHandler.obtainMessage(PAUSE_DOWN, down).sendToTarget(); } }; downTask.setOnDownListener(downListener); executorService.execute(downTask); }
例如,處理WIFI斷開產生的IO異常邏輯:
wifi斷開導致IO異常,創建MSG_ERROR消息,添加到隊列中。從隊列中取出MSG_ERROR消息并處理:取消DownThread線程,設置running為false以結束while循環,在OnDownListener接口實現類中,調用Handler對象更新數據庫表等。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。