Preface :
Recently in the bad code to write their own refactoring, the previous use of the network request is based on Apache HttpClient, simple use fortunately, the use of more than the discovery of duplicate code too much, and each use is very cumbersome, so on the internet to find a half-day network request related class library, and finally determine the use of volley, So now take note of the use notes:
Volley several main functions:
1. Ordinary string Request:
Now that network requests are in use, network permissions are essential and added in Androidmanifest.xml:
<uses-permission android:name= "Android.permission.INTERNET"/>
Because there are get/post two network requests, it is important to note that the call volley when the post is provided is the Getparams method to deposit the key value pair, so here used a string ... strkv instead of HashMap;
The code is as follows:
Packagecom.dreamyfly.volleyexample.utils;ImportCom.android.volley.AuthFailureError;Importcom.android.volley.Request;ImportCom.android.volley.Response;ImportCom.android.volley.VolleyError;Importcom.android.volley.toolbox.StringRequest;ImportJava.util.HashMap;ImportJava.util.Map; Public classHttputils {/*** How to use: * 1. Implement Requestqueue instantiation in the corresponding activity * 2. Implement the corresponding asyncrequestable and executable interfaces * 3. Fill in Get or post URL, the final request result is returned in the executable method * 4. Incoming parameters are passed in string ... strkvs format, example: "Email", "[email protected]", "Password", "123456" * so the actual format of the last incoming parameter in 4 will change to: [Email protected ]&password=123456*/ Public Static voidDovolleyget (asyncrequestable QA, String getUrl,FinalExecutable<string>execonsuccess,FinalExecutable<volleyerror>Execonerror,FinalString ... strkvs) {dovolleyrequest (Request.Method.GET, QA, GETURL, execonsuccess, Execonerror, STRKVS); } Public Static voiddovolleypost (asyncrequestable QA, String PostURL,FinalExecutable<string>execonsuccess,FinalExecutable<volleyerror>Execonerror,FinalString ... strkvs) {dovolleyrequest (Request.Method.POST, QA, PostURL, execonsuccess, Execonerror, STRKVS); }
Private Static voidDovolleyrequest (intRequestmethod, asyncrequestable QA, String Requesturl,FinalExecutable<string>execonsuccess,FinalExecutable<volleyerror>Execonerror,FinalString ... strkvs) {stringrequest Requestpost=NewStringrequest (Requestmethod, Requesturl,NewResponse.listener<string>() {@Override Public voidonresponse (String resultstr) {if(Execonsuccess! =NULL) Execonsuccess.execute (RESULTSTR); } }, NewResponse.errorlistener () {@Override Public voidonerrorresponse (Volleyerror volleyerror) {if(Execonerror! =NULL) Execonerror.execute (volleyerror); }}) {@OverrideprotectedMap<string, string> getparams ()throwsAuthfailureerror {HashMap<string, string> map =NewHashmap<string, string>(); if(Strkvs! =NULL&& (strkvs.length% 2 = = 0)) { for(inti = 0; i < strkvs.length; i + = 2) {map.put (strkvs[i], strkvs[i+ 1]); } } returnmap; } }; //Queuetag used to call Cancelall (Queuetag) to cancel a request to join a queueRequestpost.settag (Qa.getqueuetag ()); Qa.getqueue (). Add (Requestpost); }}
Use GET requests in the following ways:
//call Dovolleyget to implement a GET requestHttputils.dovolleyget (Newasyncrequestable () {@Override PublicString Getqueuetag () {return NULL; } @Override Publicrequestqueue Getqueue () {//return Mqueue to ensure Mqueue can join the Requestqueue returnMqueue; }}, Method_get_url,NewExecutable<string>() {@Override Public voidExecute (String resultstr) {//GET requests are outgoing as string and can implement UI actions, such as ToastToast.maketext (mainactivity. This, "GET Result:" +ResultStr, Toast.length_short). Show (); Requestresulttv.settext (string.valueof (RESULTSTR)); } }, NewExecutable<volleyerror>() {@Override Public voidExecute (volleyerror volleyerror) {//if access fails, print logLOG.E ("Volleyerror", volleyerror.tostring ()); } }); //Strkvs Don't preach anything, the argument is null.
2. JSON Request:
JSON interface for JSON requests I am currently using the National Weather Service Interface: http://www.weather.com.cn/adat/sk/101010100.html (on-line search, can be used), such as:
{weatherinfo: {city: "Beijing",Cityid: "101010100", Temp: "Ten",WD: "Southeast Wind", WS: "Level 2", SD: "26%" , WSE: "2" , Time: "10:25", Isradar: "1", Radar: "JC_RADAR_AZ9010_JB", Njd: "No Live", qy: "1012" }}
3. Networkimageview Load Image:
Related resources:
1. Great God Analysis: Guolin
2. Source code:
Github:https://github.com/eterrao/android-volley.git
Google:Https://android.googlesource.com/platform/frameworks/volley
CSDN Download the jar package:
I'll go ahead and get here today.
"Android" volley make several use of network request