Volly 解析VolleyError獲取包體信息

在使用Volly開源框架時遇到了一個問題,報了錯誤只有一個 SERVERERROR , 那怎么辦呢,找谷哥咯,貼上搜索下發(fā)現(xiàn)是服務(wù)器的響應(yīng)的一個錯誤,最有可能的4xx或5xx HTTP狀態(tài)代碼。
一般我們使用Volly打印日志是用 ** Log.e("VolleyError---", volleyError.getMessage(), volleyError);**
打印出的錯誤信息非常有限,如下面一些


null
com.android.volley.ServerError
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:145)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:105)


以及 Volley框架打印的錯誤


BasicNetwork.performRequest: Unexpected response code 500 for


這些也僅只能判斷是服務(wù)器端出現(xiàn)問題,但是究竟是什么原因,請求的參數(shù)不正確?還是服務(wù)器端哪里不對了?憑借以上信息個人一般無法判斷。
判斷不出,那只能想辦法了,從哪突破呢,既然是 VolleyError 拋出錯誤,那我們來分析一下拋出錯誤的VolleyError類吧。
這里開始推輪子,貼源碼

public class VolleyError extends java.lang.Exception {
    public final com.android.volley.NetworkResponse networkResponse;

    public VolleyError() { /* compiled code */ }

    public VolleyError(com.android.volley.NetworkResponse response) { /* compiled code */ }

    public VolleyError(java.lang.String exceptionMessage) { /* compiled code */ }

    public VolleyError(java.lang.String exceptionMessage, java.lang.Throwable reason) { /* compiled code */ }

    public VolleyError(java.lang.Throwable cause) { /* compiled code */ }
}

看到這里,我們猜測 networkResponse 這個對象含有返回的全部消息。

再打開 NetworkResponse 這個類的源碼。

public class NetworkResponse {
    /**
     * Creates a new network response.
     * @param statusCode the HTTP status code
     * @param data Response body
     * @param headers Headers returned with this response, or null for none
     * @param notModified True if the server returned a 304 and the data was already in cache
     */
    public NetworkResponse(int statusCode, byte[] data, Map<String, String> headers,
            boolean notModified) {
        this.statusCode = statusCode;
        this.data = data;
        this.headers = headers;
        this.notModified = notModified;
    }

    public NetworkResponse(byte[] data) {
        this(HttpStatus.SC_OK, data, Collections.<String, String>emptyMap(), false);
    }

    public NetworkResponse(byte[] data, Map<String, String> headers) {
        this(HttpStatus.SC_OK, data, headers, false);
    }

    /** The HTTP status code. */
    public final int statusCode;

    /** Raw data from this response. */
    public final byte[] data;

    /** Response headers. */
    public final Map<String, String> headers;

    /** True if the server returned a 304 (Not Modified). */
    public final boolean notModified;
}

注意看注釋,@param data Response body 于是我們知道data是回應(yīng)報文的包體內(nèi)容,只要把data解碼并顯示出來,那么就有可能看到更詳細(xì)的錯誤信息,最起碼是一段HTML文檔。
接下來我們只需要把data解碼獲?。?/p>

        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("VolleyError---", volleyError.getMessage(), volleyError);
                byte[] htmlBodyBytes = volleyError.networkResponse.data;  //回應(yīng)的報文的包體內(nèi)容
                Log.e("VolleyError body---->", new String(htmlBodyBytes), volleyError);
                return;
            }
        }

經(jīng)過這樣改造后,logcat中的錯誤輸出信息就變得很詳細(xì)和清晰了。

Volley的異常列表:

  • AuthFailureError:如果在做一個HTTP的身份驗(yàn)證,可能會發(fā)生這個錯誤。
  • NetworkError:Socket關(guān)閉,服務(wù)器宕機(jī),DNS錯誤都會產(chǎn)生這個錯誤。
  • NoConnectionError:和NetworkError類似,這個是客戶端沒有網(wǎng)絡(luò)連接。
  • ParseError:在使用JsonObjectRequest或JsonArrayRequest時,如果接收到的JSON是畸形,會產(chǎn)生異常。
  • SERVERERROR:服務(wù)器的響應(yīng)的一個錯誤,最有可能的4xx或5xx HTTP狀態(tài)代碼。
  • TimeoutError:Socket超時,服務(wù)器太忙或網(wǎng)絡(luò)延遲會產(chǎn)生這個異常。默認(rèn)情況下,Volley的超時時間為2.5秒。如果得到這個錯誤可以使用RetryPolicy。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容