volley源碼解析(七)--最終目的之Response<T>,volleyresponse

來源:互聯網
上載者:User

volley源碼解析(七)--最終目的之Response<T>,volleyresponse

在上篇文章中,我們最終通過網路,擷取到了HttpResponse對象

HttpResponse是android包裡面的一個類,然後為了更高的擴充性,我們在BasicNetwork類裡面看到,Volley將其封裝成一個Volley自己的對象NetworkResponse

另外,在BasicNetwork類中我們也注意到,對HttpResponse封裝成NetworkResponse的過程中,使用HttpResponse的Inputstream,將資料儲存在一個byte[]數組中。

BasicNetwork程式碼片段:

 // Some responses such as 204s do not have content.  We must check.                  if (httpResponse.getEntity() != null) {//返迴響應主體                    responseContents = entityToBytes(httpResponse.getEntity());//將主體轉換byte[]形式                  } else {//沒有返回內容                    // Add 0 byte response as a way of honestly representing a                    // no-content request.                    responseContents = new byte[0];                  }  
這樣可能造成的一個問題,就是記憶體溢出,這也是Volley之所以不能用來下載大檔案的原因,因為byte[]是儲存在記憶體中的。


好了,下面讓我們來看NetworkResponse的源碼

     /**      * The HTTP status code.     * http狀態代碼      */    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).     * 網頁是否修改.304      */    public final boolean notModified;    /**      * Network roundtrip time in milliseconds.     * 回應時間      */    public final long networkTimeMs;/**     * 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     * @param networkTimeMs Round-trip network time to receive network response     */    public NetworkResponse(int statusCode, byte[] data, Map<String, String> headers,            boolean notModified, long networkTimeMs) {        this.statusCode = statusCode;        this.data = data;        this.headers = headers;        this.notModified = notModified;        this.networkTimeMs = networkTimeMs;    }

本質上沒有什麼特別的,只是將HttpResponse的內容,簡單地轉移到NetworkResponse中


接下來,在響應分發過程中,request負責把NetworkResponse又封裝成Response<T>對象

NetworkDispatcher程式碼片段:

// Parse the response here on the worker thread. 解析網路響應到本地                Response<?> response = request.parseNetworkResponse(networkResponse);
至於怎麼解析,不同的request應該有自己的實現。


可能看到這裡大家有些迷糊,原因是我們找回了之前類的一些代碼

在前面的解析中,我們總是忽略這些片段,預設為全都是Response,因為在前面的過程中,理解Response之間的不同會給我們理解核心代碼帶來困擾,所以我們都跳過了。

現在源碼解析接近尾聲,我們再回頭看各種各樣的Response就豁然開朗了。

httpStack獲得的是HttpResponse,由於HttpResponse是android的內建類,我們使用起來非常不靈活(因為我們希望response都是一樣的,無論是從緩衝中取的還是網路請求的)

根據上述原因,我們有了NetworkResponse,這個代表網路請求相應,這是Volley的自訂類,這樣我們使用起來就靈活了(理論上緩衝也應該有一個CacheResponse,然而Volley沒有這樣設計)。更加重要的一點是NetworkResponse中的byte[]數組儲存了網路資料(前面說過,這是造成記憶體溢出的原因)

最後,為了統一所有的Response,我們將NetworkResponse(理論上還有一個CacheResponse)又封裝成了了Response<T>


OK,Volley解析基本到這裡就結束了。接下來的文章,將會帶大家看一下Volley最後的一部分小花絮,關於圖片載入的部分。

另外,我還會根據自己的理解,帶大家來改造Volley,使之有更多更完善的功能。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.