1.1 Volley Introduction
The 2013 Google I/O conference introduced a new network communication framework,--volley. Volley but the asynchttpclient and universal-image-loader of the advantages of the set in one, can be as very simple as asynchttpclient http communication, You can also load pictures on your network as easily as Universal-image-loader. In addition to simple and easy to use, volley in the performance of the large-scale adjustment, its design goal is very suitable for the volume of data is not small, but the traffic is frequent network operation, and for large data volume of network operations, such as downloading files, volley performance will be very bad.
Features provided by 1.2 volley
Simply put, it provides the following handy features:
Asynchronous download of JSON, image, etc.;
Ordering of network requests (scheduling)
Priority handling of network requests
Cache
Multi-level cancellation requests and activity and lifecycle linkage (all network requests are canceled at the end of activity)
1.3 Use of volley in this project
* Custom Request Queue Requestqueue
Package Com.genting.membership.singleton;
import Android.content.Context;
import com.android.volley.RequestQueue;
import Com.android.volley.toolbox.ImageLoader;
import Com.android.volley.toolbox.Volley;
import Com.googlecode.androidannotations.annotations.EBean;
import Com.googlecode.androidannotations.api.Scope;
import Com.macrokiosk.utilities.BitmapLruCache;
@EBean (scope = scope. Singleton)
Public class Volleysingleton {
Public Requestqueue Requestqueue;
Public Imageloader Imageloader;
Public Volleysingleton (Context context) {
if (context = null) {
Requestqueue = Volley. Newrequestqueue (context);
Imageloader = new imageloader (Requestqueue, new Bitmaplrucache ());
}
}
}
* Custom Task class: Jsonarrayrequest,stringrequest
Public class Jsonarrayrequest extends jsonrequest<jsonarray> {
Public Jsonarrayrequest (int method, String url,jsonobject jsonrequest,
Listener<jsonarray>listener, Errorlistener Errorlistener) {
Super (Method, url, (jsonrequest = = null)? null : Jsonrequest.tostring (),
Listener, Errorlistener);
}
@Override
protected Response<jsonarray> parsenetworkresponse (Networkresponse Response) {
Try {
Stringjsonstring =
New String (Response.data, Httpheaderparser. Parsecharset(response.headers));
return Response. Success (newJsonarray (jsonstring),
Httpheaderparser. parsecacheheaders (response));
} catch(Unsupportedencodingexception e) {
return Response. Error (new parseerror (e));
} Catch (jsonexception je) {
return Response. Error (new parseerror (JE));
}
}
@Override
Public Retrypolicy Getretrypolicy () {
retrypolicymretrypolicy=New defaultretrypolicy (60000, 1,1.0f);
return Mretrypolicy;
}
}
* Load task, send request
Jsonarrayrequest request=New jsonarrayrequest (Method). GET, Constants. basic_url,null, Geturlsuccess (), GetError ("Getbasicurl"));
Request.setretrypolicy (newdefaultretrypolicy (1, 1.0f));
Mrequestqueue.add (Request);
* Cancel Request Task
Soaprequest.settag (this), where this is the label of the set
MVolley.requestQueue.cancelAll (this), where this is the label just set
Use of Volley.jar