This article is used to keep track of some of the stringrequest I have recently used volley, as well as the volley of the code for the request.
As you can see from the network, volley is suitable for scenarios where the data is small but frequently communicated. The convenience features provided by Volley are as follows:
- Asynchronous download of JSON, image, etc.;
- Ordering of network requests (scheduling)
- Priority handling of network requests
- Cache
- Multi-level cancellation requests
- Interaction with activity and life cycle (simultaneous cancellation of all network requests at end of activity)
From some network predecessors and their own use of the situation, volley picture processing is not the best, I chose the Picasso, so I only use the request, and the activity life cycle of the linkage two features.
Previously wrote a volley first contact, mainly Volley.newrequestqueue (Getapplicationcontext ()), this code interpretation.
The following begins the interpretation of Stringrequest.
The network of predecessors all Stringrequest request as a custom request, and Jsonobjecy jsonarray difference Open, I also do not know why this distinction, for the moment no use volley JSON request, but read the introduction, It feels like all the request is the same.
Use a scenario to parse a piece of XML data using a Stringrequest request.
Stringrequest request = new Stringrequest ( Request.Method.GET, contants.http_server + contants.http_tabs, New Response.listener<string> () {
@Override
public void Onresponse (String response) {
This handles the response returned by the request.
... ...
}
}, new Response.errorlistener () {
@Override
public void Onerrorresponse (Volleyerror error) {
Work here to handle the failed request
LOGUTILS.D (Tag,error.tostring ());
}
}
);
Or a code, all the parameters used to appear, now to stringrequest look at the situation
public class Stringrequest extends request<string> {private final listener<string> mlistener; Public stringrequest (int method, String URL, listener<string> Listener, Errorlistener errorlistener) { Super (method, URL, errorlistener);//Here is called super not thisMlistener = listener; } public stringrequest (String URL, listener<string> Listener, Errorlistener errorlistener) {This (METHOD.G ET, URL, Listener, errorlistener); } @Override protected void Deliverresponse (String response) {mlistener.onresponse (response);//Set our listener} @Override protected response<string> parsenetworkresponse (Networkresponse Response) {
//literal parsing of network responses String parsed; try {parsed = new String (Response.data, Httpheaderparser.parsecharset (response.headers)); } catch (Unsupportedencodingexception e) {parsed = new String (response.data); } return Response.success (parsed, httpheaderparser.parsecacheheaders (Response)); }
The Stringrequest class does not have much code, but is an important part of it.
Here's a record of the difference between the first two points super (method, URL, Errorlistener) and Parsenetworkresponse & Mlistener.onresponse (response).
First is super (method, URL, errorlistener): Stringrequest inherit request<string>, jump to Request<string>
Public Request (int method, String URL, Response.errorlistener listener) { Mmethod = method; Murl = URL; Merrorlistener = listener; Setretrypolicy (New Defaultretrypolicy ()); Mdefaulttrafficstatstag = Finddefaulttrafficstatstag (URL); }
The request saved the submission method, the URL of the connection, the error listener interface, and set the retry policy, and found the communication status flag from the URL (what's the use?). )
Setting the retry policy it feels good to understand, see the Defaultretrypolicy class to know seven or eight points, the role is probably set the URL connection failed retry some of the properties (my own brain-_-!!!)
Public Defaultretrypolicy () {This (Default_timeout_ms, default_max_retries, Default_backoff_mult); } ... /** * Constructs a new retry policy. * @param INITIALTIMEOUTMS The initial timeout for the policy. * @param maxnumretries The maximum number of retries. * @param backoffmultiplier Backoff multiplier for the policy. * /public defaultretrypolicy (int initialtimeoutms, int maxnumretries, float backoffmultiplier) { Mcurrenttimeoutms = Initialtimeoutms; Mmaxnumretries = maxnumretries; Mbackoffmultiplier = Backoffmultiplier; }
The remaining Mdefaulttrafficstatstag = Finddefaulttrafficstatstag (URL); the function of the Javadoc explanation returns the hashcode of the URL ' s host component, or 0 if there is none, this It's not too good to be a brain tonic, or a look at the implementation of a function.
private static int finddefaulttrafficstatstag (String URL) { if (! Textutils.isempty (URL)) { uri uri = uri.parse (URL); if (uri! = null) { String host = Uri.gethost (); if (host! = null) { return host.hashcode () ; }}} return 0; }
See the surprisingly simple code, I think of the volley cache mechanism, I heard is simple to determine whether the URL is the same, is this?? Is not this also unimportant, anyway Stringrequest initialization attribute as above.
The rest is deliverresponse and parsenetworkresponse, and then I found an article on the internet that was quite clear.
Http://www.apihome.cn/view-detail-70213.html
I use my own vernacular summed up: Parsenetworkresponse is used for a specific type (String, Json, Jsonarray, Image) The resolution of the request, the data returned by the server to the specified type, Then the system callback Deliverresponse to dispatch the corresponding instance. Not very clear can look at the above types of request, which are used parsenetworkresponse to convert networkresponse into the corresponding type.
Note: Before I wrote this article, I was directly in the initialization of the stringrequest request, I did not rewrite Parsenetworkresponse, directly to the server side of the original string, And then in the Response.listener in the onresponse of processing, feel not very native. Thanks to the seniors on the network, thank themselves.
Interpretation of Android.volley: request