The error handling logic during the download process is:
1. if an exception is found in the run method of DownThread, call sendMessage (int type, int data) to create a message and add it to the queue.
@ Overridepublic 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 (downask. connectTimeout); conn. setRequestProperty ("Range", "bytes =" + start + "-" + end); int responseCode = conn. getResponseCode (); Logger. d (TAG, "the byte range of the downloaded Resource :" + 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]; // 20 k cache 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 ();} finall Y {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. Retrieve and process the messages in the queue in the while loop of downask's run method. Messages of different categories are processed separately. Message categories include MSG_SUCCESS, MSG_ERROR, MSG_UPDATE, and 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);
Because downListener. onDownError is widely used, it is extracted and stored in the policydownerror method.
/*** Download error ** @ param type */private void policydownerror (int type) {if (downListener! = Null) downListener. onDownError (type );}
3. The OnDownListener interface is implemented in DownService. In the interface method, the Handler object of the handle is called to process different types of messages. The processing operations include updating the database table and displaying the Toast prompt.
The OnDownListener interface is implemented when a download task is started.
/*** Start a download task. ** @ Param down * @ param downask */public void launchDownloadTask (final Down down, downask) {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 () ;}}; downask. setOnDownListener (downListener); executorService.exe cute (downask );}
For example, to handle the IO exception logic caused by WIFI disconnection:
An IO exception occurs when wifi is disconnected. Create an MSG_ERROR message and add it to the queue. Retrieve and process the MSG_ERROR message from the queue: cancel the DownThread and set running to false to end the while loop. In the OnDownListener interface implementation class, call the Handler object to update the database table.