The previous article introduced the use of volley, the main contact with the request and requestqueue the two classes, this article to understand the specific implementation of these two classes.
Request Class Diagram:
Request class: Request is an abstract class with the main properties:
Mmethod: Request method, currently supports GET, POST, PUT, DELETE, HEAD, Options,trace, Patch method
Murl: Request URL
Merrorlistener: Error handling listener, calling when request error occurs
Msequence: The sequence number of the request, the same priority request is sorted by ordinal in the request queue, and the lower ordinal is in front of the queue.
Mrequestqueue: The request queue where the request is located
Mcacheentry:when a request can be retrieved from cache but must is refreshed from the network, the cache entry would be St ORed here's in the event of a "not Modified" response, we can be sure it hasn ' t been evicted from cache.
Mretrypolicy: Retry policy for the request
Mshouldcache: Whether the response of the request is cached
Mcanceled: Whether the request can be canceled
Mresponsedelivered: Whether the requested response has been delivered.
There is also a Mtag attribute, which is used to mark the request and can be canceled in bulk by tag in Requestqueue.
The main methods include:
GetBody (): Returns the byte array representation of the request body. The default implementation returns NULL, so if it is a post or put request, the subclass needs to override this method.
Parsenetworkresponse () and deliverresponse () are abstract methods that are implemented by subclasses. Parsenetworkresponse is used to parse the original response information and return a specific response type, which is the result of type T in response<t>. The deliverresponse is used to deliver parsed response results to the listener for processing.
By default, the priority of request is normal, the following source code:
1 /**2 * Priority values. Requests'll be processed from higher priorities to3 * Lower priorities, in FIFO order.4      */5      Public enumPriority {6 Low ,7 NORMAL,8 High ,9 IMMEDIATETen     } One  A     /** - * Returns the {@linkPriority} of this request; {@linkPriority#normal} by default. -      */ the      PublicPriority GetPriority () { -         returnPriority.normal; -}
The request implements the comparable interface to compare the priority of the request to determine the order of the request in the queue. The higher the priority, the lower the ordinal of the same priority in the request queue, and the earlier the order is higher.
1 /**2 * Our comparator sorts-from-low-priority, and secondarily by3 * Sequence number to provide FIFO ordering.4      */5 @Override6      Public intCompareTo (request<t>Other ) {7Priority left = This. getpriority ();8Priority right =other.getpriority ();9 Ten         //high-priority Requests is "lesser" so they is sorted to the front. One         //Equal priorities is sorted by sequence number to provide FIFO ordering. A         returnleft = = right? -                  This. msequence-other.msequence: -Right.ordinal ()-left.ordinal (); the}
Request derives three subclasses of Jsonrequest, Imagerequest, Clearcacherequest. which
1. Jsonrequest<t> is also an abstract class that can send a JSON-represented request body and return a T-type response, mainly including
Mlistener: Listener for Successful request
Mrequestbody: The JSON string representation of the request body.
Parsenetworkresponse () is still an abstract method, GetBody () returns a byte array of mrequestbody. The Onresponse method of Mlistener is called in Deliverresponse , which is defined by the user. Refer to the previous article.
1 @Override 2     protected void Deliverresponse (T response) {3        mlistener.onresponse (response); 4     }
Jsonarrayrequest, jsonobjectrequest inherit from Jsonrequest, which represents the request to return a Jsonarray response and a request to return a jsonobject response. The parsenetworkresponse implementation of Jsonarrayrequest, Jsonobjectrequest, is similar to that of returning a jsonobject type of response, rather than Jsonarray
1 @Override2     protectedResponse<jsonarray>Parsenetworkresponse (networkresponse response) {3         Try {4String jsonstring =5                 NewString (Response.data, Httpheaderparser.parsecharset (response.headers));6             returnResponse.success (NewJsonarray (jsonstring),7 httpheaderparser.parsecacheheaders (response));8}Catch(unsupportedencodingexception e) {9             returnResponse.error (NewParseError (e));Ten}Catch(jsonexception je) { One             returnResponse.error (NewParseError (JE)); A         } -}
2. Imagerequest is used to request a bitmap bitmap based on a URL, including attributes
Mlistene: Listener to receive decoded bitmap
Mmaxwidth: Decodes the maximum width of the bitmap,
Mmaxheight: Decoding the maximum height of a bitmap
If Mmaxwidth,mmaxheight is 0, the original size of the bitmap is maintained, and if one of them is not 0, it is decoded according to the aspect ratio of the original bitmap, and if none is 0, it is decoded to the bitmap that best fits the width x Height area and preserves the original bitmap aspect ratio.
The Imagerequest priority is the lowest.
1 @Override 2      Public Priority GetPriority () {3         return Priority.low; 4     }
3. Clearcacherequest a simulated request to clean up the cache
Mcache: Cache required for cleanup
Mcallback: The callback interface that is called in the main thread after the cache has been cleaned up.
Both Parsenetworkresponse and Deliverresponse are empty implementations, as this is a mock request and there is no actual network request.
The clearcacherequest is the highest priority.
1 @Override 2      Public Priority GetPriority () {3         return priority.immediate; 4     }
It's not too early, I'll be here today. Continue tomorrow.