Volley Source code Analysis (vii)--response<t> of the ultimate purpose

Source: Internet
Author: User

In the previous article, we ended up getting the HttpResponse object through the network

HttpResponse is a class within the Android package, and then for higher extensibility, we see in the Basicnetwork class that volley wraps it into a volley own object Networkresponse

In addition, we also notice in the Basicnetwork class that the HttpResponse is packaged into Networkresponse, and the data is saved in an InputStream] array using the byte[of the HttpResponse.

Basicnetwork Code Snippet:

Some responses such as 204s does not have content.  We must check.                  if (httpresponse.getentity () = null) {//Returns the response body                    responsecontents = entitytobytes (Httpresponse.getentity ());// Convert principal to byte[] form                  } else {//no return content                    //ADD 0 byte response as a way of honestly representing a                    //no-content reques T.                    responsecontents = new byte[0];                  }  
One of the possible problems is memory overflow, which is why volley cannot be used to download large files because byte[] is stored in memory.


Okay, let's see Networkresponse's source code.

     /**      * The HTTP status code.     * HTTP state code      */&NB sp;   Public final int statuscode;   /**      * RAW data from this response.      * Data      */    public final byte[] data;    /**      * Response headers.     * response header      */      public final map<string, string> headers;   /**      * True if the Server returned a 304 (not Modified) .     * Web page is modified. 304      */     Public Final Boolean notmodified;   /**      * Network roundtrip time in Millis econds.     * Response Time      */    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 RESP Onse, or null for none * @param notmodified True If the server returned a 304 and the data is 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.statusco        de = StatusCode;        This.data = data;        This.headers = headers;        this.notmodified = notmodified;    This.networktimems = Networktimems; }

There is nothing special in nature, simply transferring the contents of HttpResponse to Networkresponse


Next, in response to the distribution process, the request is responsible for wrapping the networkresponse into a Response<t> object

Networkdispatcher Code Snippet:

Parse the response here on the worker thread. Parse the network response to local                response<?> Response = Request.parsenetworkresponse (networkresponse);
As for how to parse, different request should have its own implementation.


You might see some confusion here because we got back some code from the previous class.

In the previous parsing, we always ignore these fragments, the default is all response, because in the previous process, understanding the difference between the response will give us the understanding of the core code, so we all skipped.

Now the source parsing is nearing the end, we look back at all kinds of response on the enlightened.

Httpstack get HttpResponse, because HttpResponse is Android's built-in class, we use it very inflexible (because we want the response to be the same, whether it is taken from the cache or the network request)

According to the above reasons, we have networkresponse, this represents the network request corresponding, this is the volley custom class, so that we can use it flexibly (in theory, the cache should have a cacheresponse, but volley is not so designed). A more important point is that the byte[in Networkresponse] array holds the network data (this is the cause of the memory overflow, as mentioned earlier)

Finally, in order to unify all the response, we will networkresponse (theoretically a cacheresponse) and encapsulate the response<t>


Ok,volley parsing is basically here to end. The next article, will take you to see the last part of volley small tidbits, about the picture loading part.

In addition, I will also according to their own understanding, bring you to transform volley, so that there are more and more perfect function.


Volley Source code Analysis (vii)--response<t> of the ultimate purpose

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.