1. Framework Features
(1). Communication is faster and easier
(2). Get, Post network requests, and high efficiency asynchronous processing requests for network images
(3). Sort
(4). Caching of Network requests
(5). Multi-level cancellation requests
(6). Linkage with activity life cycle
Cons: not suitable for uploading and downloading
Advantages: Efficient Get/post Way of data request interaction, network picture loading and caching, is Google's official launch of the framework, the performance is very stable and strong.
2. Network data requests
(1). Request data Using Get method
To create the application class:
<span style= "White-space:pre" ></span>public class MyApplication extends application {public static requestqueue queues; @Overridepublic void OnCreate () {super.oncreate (); queues = Volley.newrequestqueue ( Getapplicationcontext ());} public static Requestqueue Gethttpqueues () {return queues;}}
To test a GET request with Stringrequest:
<span style= "White-space:pre" ></span>private void Volleygetstringreuest () {String URL = "/http/ Apis.juhe.cn/mobile/get?phone=13689249616&key=d83212f9ca3a6028fa0d7d77a3ff3bf8 ";//Create Request Object Stringrequest Request = new Stringrequest (Method.get, Url,new listener<string> () {//Call @overridepublic void Onresponse ( String arg0) {toast.maketext (mainactivity.this, arg0, 0). Show ();}}, New Response.errorlistener () {// Call @overridepublic void Onerrorresponse (Volleyerror arg0) {toast.maketext (Mainactivity.this, arg0.tostring (), 0) when the request fails . Show ();}}); Request.settag ("Abcget"); Myapplication.gethttpqueues (). Add (request);}
To test a GET request with Jsonobjectrequest:
private void Volleygetjsonobjectreuest () {String URL = "http://apis.juhe.cn/mobile/get?phone=13689249616&key= D83212f9ca3a6028fa0d7d77a3ff3bf8 ";//Create Request object Jsonobjectrequest ask = new Jsonobjectrequest (Method.get, Url,null, New Listener<jsonobject> () {//request succeeded @overridepublic void Onresponse (Jsonobject arg0) {Toast.maketext ( Mainactivity.this, arg0.tostring (), 0). Show ();}, new Response.errorlistener () {//request failed @overridepublic void Onerrorresponse (Volleyerror arg0) {toast.maketext (Mainactivity.this, arg0.tostring (), 0). Show ();}); Request.settag ("Abcget"); Myapplication.gethttpqueues (). Add (request);}
(2). Using Post to request data
To test a POST request using Stringrequest:
private void Volleystringrequestpost () {String url = "Http://apis.juhe.cn/mobile/get?"; Stringrequest request = new Stringrequest (Method.post, Url,new listener<string> () {@Overridepublic void Onresponse (String arg0) {toast.maketext (mainactivity.this, arg0, 0). Show ();}}, New Response.errorlistener () {@ overridepublic void Onerrorresponse (Volleyerror arg0) {toast.maketext (Mainactivity.this, arg0.tostring (), 0). Show ();}}) {@Overrideprotected map<string, string> getparams () throws Authfailureerror {map<string, string> Map = new Ha Shmap<string, String> (), Map.put ("Phone", "13002909620"), Map.put ("Key", "D83212f9ca3a6028fa0d7d77a3ff3bf8"); return map;}}; Request.settag ("Abcget"); Myapplication.gethttpqueues (). Add (request);}
3. Volley and activity linkage
(1). Linkage between volley and activity
Overwrite the OnStop method:
@Overrideprotected void OnStop () {super.onstop (); Myapplication.gethttpqueues (). Cancelall ("Abcget");}
(2). Two callback packages for volley
The first thing to do is to create an abstract class Volleyinterface:
Public abstract class Volleyinterface {public Context mcontext;public listener<string> mlistener;public Errorlistener merrorlistener;public abstract void onmysuccess (String result);p ublic abstract void Onmyerror ( Volleyerror error);p ublic volleyinterface (context context, listener<string> Listener,errorlistener Errorlistener) {Mcontext = Context;mlistener = Listener;merrorlistener = Errorlistener;} Public listener<string> Loadinglistener () {mlistener = new listener<string> () {@Overridepublic void Onresponse (String arg0) {onmysuccess (arg0);}}; return Mlistener;} Public Errorlistener Errorlistener () {merrorlistener = new Errorlistener () {@Overridepublic void Onerrorresponse ( Volleyerror arg0) {onerrorresponse (arg0);}}; return merrorlistener;}}
Then create a class that customizes our get and POST requests:
public class Volleyrequest {public static stringrequest stringrequest;public static Context context;/** * Custom Get * * @par AM context * @param URL * @param tag * @param vif */public static void Requestget (context context, string url, string tag, Volleyinterface vif) {myapplication.gethttpqueues (). Cancelall (tag); stringrequest = new Stringrequest (Method.GET, URL , Vif.successlistener (), Vif.errorlistener ()); Stringrequest.settag (tag); Myapplication.gethttpqueues (). Add (Stringrequest); Myapplication.gethttpqueues (). Start ();} /** * Custom Post * * @param context * @param URL * @param tag * @param params * @param vif */public static void Requestpost ( Context context, string URL, String tag,final map<string, string> params, volleyinterface vif) {myapplication.getht Tpqueues (). Cancelall (tag), stringrequest = new Stringrequest (Method.post, Url,vif.successlistener (), Vif.errorlistener ()) {@Overrideprotected map<string, string> getparams () throws Authfailureerror {return params;}}; Stringrequest.settag (tag); Myapplication.gethttpqueues (). Add (Stringrequest); Myapplication.gethttpqueues (). Start ();}}To modify the code for the test:
/** * Custom Get mode */private void MyGet () {String URL = "http://apis.juhe.cn/mobile/get?phone=13689249616&key= D83212f9ca3a6028fa0d7d77a3ff3bf8 "; Volleyrequest.requestget (this, URL, "Abcget", New Volleyinterface (This,volleyinterface.mlistener, Volleyinterface.merrorlistener) {@Overridepublic void onmysuccess (String result) {Toast.maketext (Mainactivity.this, result, 0). Show ();} @Overridepublic void Onmyerror (volleyerror error) {Toast.maketext (Mainactivity.this, error.tostring (), 0). Show ();}});
The advantage of this is that we can write both the work and the failure in volleyinterface so that it can be used by every caller.
I won't do the test about post.
4. Get a picture of the network
(1). Get network pictures using Imagerequest
public class Imageactivity extends Activity {private ImageView ImageView; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_image); InitView (); String url = "Https://www.baidu.com/img/bdlogo.png";//width and height specify 0 and 0 to load imagerequest request = new Imagerequest (URL, New Listener<bitmap> () {@Overridepublic void Onresponse (Bitmap arg0) {imageview.setimagebitmap (arg0);}}, 0, 0, config.rgb_565, New Response.errorlistener () {@Overridepublic void Onerrorresponse (Volleyerror arg0) { Imageview.setimageresource (R.drawable.ic_launcher);}}); Myapplication.gethttpqueues (). Add (request);} private void Initview () {ImageView = (ImageView) Findviewbyid (r.id.image);}}
(2). Cache network images using Imageloader and Imagelistener
To create the Bitmapcache class:
public class Bitmapcache implements Imagecache {public lrucache<string, bitmap> cache;public int max = 10 * 1024 * 1 024;public Bitmapcache () {cache = new lrucache<string, bitmap> (max) {@Overrideprotected int sizeOf (String key, BITM AP value) {return value.getrowbytes () * Value.getheight ();}};} @Overridepublic Bitmap Getbitmap (String arg0) {return cache.get (arg0);} @Overridepublic void Putbitmap (String arg0, Bitmap arg1) {cache.put (arg0, arg1);}}
To invoke the test:
private void Testimageloader (String url) {Imageloader loader = new Imageloader (Myapplication.gethttpqueues (), New Bitmapcache ()); Imagelistener listener = Imageloader.getimagelistener (Imageview,r.drawable.ic_launcher, R.drawable.ic_launcher); Loader.get (URL, listener, 0, 0);}
(3). Load pictures with Imageloader and Networkimageview:
private void Testnetworkimageview (String url) {Imageloader loader = new Imageloader (Myapplication.gethttpqueues (), New Bitmapcache ()); Netimageview.setdefaultimageresid (R.drawable.ic_launcher); Netimageview.seterrorimageresid ( R.drawable.ic_launcher); Netimageview.setimageurl (URL, loader);}
SOURCE download
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Android Advanced" (2) using the Open source framework volley