Volley was released at the 2013 Google I/O 2013 conference, which is faster, easier, and more convenient for our network communications. It's a good frame for beginners.
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
- Interaction with activity and life cycle (simultaneous cancellation of all network requests at end of activity)
First, get the JSON object |
1.1 Declaration Requestqueue
Declares a new Requestqueue object
Initialize Mrequestqueue in OnCreate
Mrequestqueue = Volley.newrequestqueue (this);
Declare and use the request
Jsonobjectrequest Jr = New Jsonobjectrequest (request.method.get,url,null,new response.listener<jsonobject> () { @Override public void Onresponse (jsonobject response) { log.i (tag,response.tostring ()); Parsejson (response); Va.notifydatasetchanged (); Pd.dismiss (); } },new Response.errorlistener () { @Override public void Onerrorresponse (volleyerror Error) { log.i (tag,error.getmessage ()); } }); Mrequestqueue.add (JR);
Volley provides jsonobjectrequest, Jsonarrayrequest, Stringrequest and other request forms.
Jsonobjectrequest: Returns the JSON object.
Jsonarrayrequest: Returns Jsonarray.
Stringrequest: Returns a string so that it can handle the data itself more flexibly.
Second, load the network picture |
Private Button mybtn; Private ImageView Myimageview; Private Imageloader Imageloader; Private Requestqueue Requestqueue; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_image_loader); Requestqueue= Volley.newrequestqueue (Getapplicationcontext ());//Request Queue Imageloader=new Imageloader (RequestQueue, new Bitmapcache ()); Mybtn= (Button) Findviewbyid (R.ID.MYIMAGELOADERBTN); myimageview= (ImageView) Findviewbyid (R.id.myimageloader); Mybtn.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) { Imageloader.imagelistener listener = Imageloader.getimagelistener (Myimageview,r.drawable.ic_launcher, R.drawable. Ic_launcher);//The following two parameters are the default diagram loaded and the picture Imageloader.get to be displayed after loading error ("http://192.168.191.1:8080/JerehEdu/image/logo.jp G ", listener,200,200); } });}
When loading a picture, we used a class like Bitmapcache, which we inherited from the imagecache implementation of volley, which allows us to cache the images we load.
public class Bitmapcache implements Imagecache { private lrucache<string, bitmap> Mcache; Public Bitmapcache () { int maxSize = ten * 1024x768 *; Mcache = new lrucache<string, bitmap> (maxSize) { @Override protected int sizeOf (String key, Bitmap value) { //TODO auto-generated method stub return value.getrowbytes () * Value.getheight (); } ; } @Override public Bitmap getbitmap (String arg0) { //TODO auto-generated method stub return Mcache.get ( arg0); } @Override public void Putbitmap (String arg0, Bitmap arg1) { //TODO auto-generated method stub Mcache.put (arg0, arg1);} }
Now applied to the network communication framework has a lot like volley so many, recently also in the study of Xutils, feel this is also very good, but also hope to exchange experience with you.
Introduction to the Volley framework uses