標籤:android style blog http io ar color os 使用
轉載註明出處 :http://blog.csdn.net/codingandroid/article/details/41749581
之前一篇中有使用到AsyncBaseRequest 類,其中拋出了很多異常,當然已經有了部分注釋,那下面我們來分析一下這些異常,以及異常出現的情況
ConnectTimeoutException 這個是串連異常(例如根本沒有開啟網路等情況下),資料還沒有到達伺服器,所以就算是訂單類的也沒有關係,可以再次提交
這個情況比較好處理了,就是請求壓根沒到伺服器,如果要類比這個錯誤最好的辦法就是,你區域網路開啟服務,把手機改為3g模式下,或者填寫一個壓根不存在的 服務地址。當然現實情況下也可能出現,訊號極差的情況下等,這種情況出現,提示逾時,讓使用者再來一次就OK.
SocketTimeoutException 這個逾時異常是說明請求已經到達伺服器,返回資料過程中逾時了,如果是訂單類的請求要注意了,需要防止重複提交
這個異常出現情境就是 例如你的逾時時間是1s,當資料到達伺服器,伺服器1s還沒有能夠順利的返回資料,這時候已經到達了用戶端設定的逾時時間,那麼,這個請求就逾時了,但是注意,你的請求這時候已經提到了伺服器,雖然你本地異常已經拋出了,伺服器這時候依然在處理,如果你是去修改的某個欄位,伺服器可能已經修改成功了,而你本地看到的是異常,這時候可以把逾時時間放大一點,或者伺服器處理資料的速度需要去最佳化
TimeoutException 這個是前兩個的父類 也就是說你如果要求不是很嚴格的情況下,可以忽略上面兩種,直接捕獲這個,當然作為架構,我希望能詳細一點,要不直接捕獲一個Exception就完事兒了,當然捕獲了前面兩個,我們還擔心會有其他的TimeOut情況呢,所以還是再捕獲一下,以防萬一
HttpHostConnectException 這個異常就不說了,通常是你沒有加許可權
NoHttpResponseException 這個是DefaultHttpClient 會出現的,屬於android下Httpclient的一個缺陷 可以參考 http://stackoverflow.com/questions/2052299/httpclient-on-android-nohttpresponseexception-through-umts-3g 不過
HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), false);
這個我加了,貌似不管用 ,然後上面這種重試的方法貌似也沒有起作用,感興趣的朋友可以研究一下哦,我遇到就直接 抓住再拋出一個 TimeOut異常出去了
EOFException 這個異常就頭疼了,是HttpURLConnection 的缺陷 http://bbs.csdn.net/topics/390143086這個是csdn的文章 還有 http://stackoverflow.com/questions/15411213/android-httpsurlconnection-eofexception 幾乎所有的處理方法我都加上了,還是機率性的會出現這個異常,那不管了,直接拋出TimeOut 糊弄糊弄使用者吧,不過我們開發人員自己要瞭解哦
IOException 流讀取的時候錯誤了,那時候我們認為是逾時 然後其他異常就不去管了
package com.clxu.netframe.exception;import java.io.IOException;import java.net.SocketTimeoutException;import java.util.LinkedList;import java.util.concurrent.TimeoutException;import org.apache.http.conn.ConnectTimeoutException;import org.apache.http.conn.HttpHostConnectException;import android.util.Log;import com.clxu.netframe.R;import com.clxu.netframe.constant.Constant;/** @類名: MyException * @功能描述: TODO(自訂異常基類) * @建立人: Clxu * @建立時間: 2014-5-28 上午8:24:56 */public class MyException extends Exception { private static final long serialVersionUID = 7803623314130181679L; /** * 錯誤異常代碼 */ protected int errorCode = -1; /** * 介面異常提示資訊 */ private int msg; /** * 開發人員查看的簡單異常資訊 */ protected int errorMsg; /** * 開發人員查看的簡單異常資訊 */ protected String errorMsgInfo; /** * 原始異常資訊 */ protected Exception oldException; /** * 異常鏈(捕獲一個地方拋出的多個異常) */ protected LinkedList<Exception> list = new LinkedList<Exception>(); public MyException () {}; public MyException (String message) { super(message); errorMsgInfo=message; } public MyException (int message) { this.setMsg(message); this.errorMsg = message; }; public MyException (int message, int errorCode) { this.setMsg(message); this.errorMsg = message; this.errorCode = errorCode; }; public MyException (int message, int errorCode, int errorMsg) { this.setMsg(message); this.errorMsg = errorMsg; this.errorCode = errorCode; }; /** * 應該new子類,誰使用誰定義 * @param e */ public MyException (Exception e) { if (e instanceof IOException) { this.errorCode = Constant.NETWORK_REQUEST_IOEXCEPTION_CODE; this.setMsg(R.string.network_request_ioexception); this.errorMsg = R.string.network_request_ioexception; } else if (e instanceof SocketTimeoutException) {//逾時 this.errorCode = Constant.NETWORK_REQUEST_SOCKET_EXCEPTION; this.setMsg(R.string.network_request_ioexception); this.errorMsg = R.string.network_request_ioexception; } else if (e instanceof ConnectTimeoutException) {//逾時 this.errorCode = Constant.NETWORK_REQUEST_CONNECT_EXCEPTION; this.setMsg(R.string.network_request_ioexception); this.errorMsg = R.string.network_request_ioexception; } else if (e instanceof TimeoutException) {//逾時 this.errorCode = Constant.NETWORK_REQUEST_TIMEOUT_EXCEPTION; this.setMsg(R.string.network_request_ioexception); this.errorMsg = R.string.network_request_ioexception; }else if (e instanceof HttpHostConnectException) { this.errorCode = Constant.NETWORK_REQUEST_PERRMIT_EXCEPTION; this.setMsg(R.string.network_request_ioexception); this.errorMsg = R.string.network_request_ioexception; }else if (e instanceof Exception) { this.errorCode = Constant.NETWORK_REQUEST_UNKNOWN_EXCEPTION; this.setMsg(R.string.system_exception); this.errorMsg = R.string.unknown_exception; } this.oldException = e; printMessage(e); } /** * 將異常添加到異常鏈 * @param e */ public void addException(Exception e) { list.add(e); } /** * 獲得最底層的異常 * @return */ public Exception getFirstException() { if(list.size() > 0) return list.get(0); else return null; } /** * 列印異常資訊 */ public void printMessage(Exception e) { e.printStackTrace(); } /** * 記錄異常資訊 */ public void logMessage() { Log.e("Exception",getDetailMessage()); } /** * 獲得異常代碼(介面層直接調用getMessage或showMessage即可) * @return */ public int getErrorCode() { return errorCode; } /** * 獲得開發人員查看的簡單異常資訊 * @return */ public int getErrorMsg() { return errorMsg; } /** * 獲得原始異常資訊 * @return */ public String getDetailMessage() { return oldException.getMessage(); } /** * 獲得異常鏈 * @return */ public LinkedList<Exception> getList() { return list; } public int getMsg() { return msg; } public void setMsg(int msg) { this.msg = msg; } public String getErrorMsgInfo(){ if(msg==0){ return errorMsgInfo; }else{ return "resource not found"; } }}
詳細看文章的會說為什麼這裡面沒有NoHttpResponseException EOFException 這兩個異常 ,這兩個異常我是在請求網路的時候直接去處理的,抓到之後直接拋出 Timeout
try {
response = client.execute(post);
} catch (NoHttpResponseException e) {
throw new TimeoutException();
}
這樣去處理的哦,好了,異常就說到這裡了
架構源碼:http://download.csdn.net/detail/brightshadow11111/8228287
贈送 HttpClient 和HttpURLConnection 的輕型網路 ---》常見異常分析