1. Recall
Example of a movie list written by Jsonobjectrequest and Imageloader using volley
2. Focus
(1) Encapsulation volley internal request class (request queue, data request, picture request, picture cache)
(2) Encapsulating Response.listener and Response.errorlistener callback functions
(3) Usage
3. Introduction to Folders
3.1 Gsonrequset.java
Define your own Gson request and instantiate the JSON string directly as an object
3.2 Volleyapplication.java
Inherit application, Singleton mode create Requestqueue
3.3 Volleybitmapcache.java
Combine with Lrucachae to achieve picture caching so that memory overflow does not occur
3.4 Volleyhandler.java
Implements Response.lintener and Response.errorlinstener monitoring, and returns onsuccess and OnError two callback functions
3.5 Volleyhttppath.java
Encapsulates the request address, which encapsulates the request address in this area. Unified Management Request Address
3.6 Volleyhttprequest.java
Encapsulates all requests to call the request queue. Data requests. Picture Request
3.7 README
Usage
4.VolleyHandler callback function class implementation
4.1 Using an abstract class implementation
4.2 Declaration of two abstract methods onsuccess and OnError
4.3 Implementation of Response.listener and Response.errorlistener
4.4 Demo Examples:
Package Com.example.volley;import com.android.volley.response;import com.android.volley.volleyerror;/** * abstract out Successful monitoring and failed listening * used to callback information * @author yuan * @param <T> */public abstract class Volleyhandler<t> {public respon Se. listener<t> reqlis;public response.errorlistener reqerr;public Volleyhandler () {//Initialize variable reqLis = new ReqListener ( ); reqerr = new Reqerrorlistener ();} public abstract void Reqsuccess (T response);p ublic abstract void Reqerror (String error);/** * After successful listening * * @author yuan * */public class Reqlistener implements response.listener<t> {@Overridepublic void Onresponse (T Response) {//Use abstract letter Number Set callback function reqsuccessreqsuccess (response);}} /** * Monitoring after failure * * @author yuan * */public class Reqerrorlistener implements Response.errorlistener {@Overridepublic Voi D onerrorresponse (volleyerror error) {//Set callback function using abstract method Reqerrorreqerror (Error.getmessage ());}}}
The rest of the implementation I will not explain, can be downloaded using: http://download.csdn.net/detail/lablenet/9035389
5. How to use
5.1 Preparing a piece
Download Volley.jar Package: Volley.jar
Download my two-time package class: Volley two times package
5.2 Configuring the request queue
The first line under the application tag in Androidmanifest.xml: New Add (the package name for Volleyapplication in name)
Android:name= "Com.example.volley.VolleyApplication"
Example:
<application <span style= "color: #ff0000;" >android:name= "Com.example.volley.VolleyApplication" </span> android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme" > <activity android:name= ". Mainactivity " android:label=" @string/app_name "> <intent-filter>
5.3 Basic Data requests (STRING,JSONOBJECT,GSON,BITMAP, etc.)
Instantiate the Volleyhandler class, for example:
The request here is a string so T is passed to string, the picture uses imagerequest words to refer to: Bitmap
Volleyhandler<string> volleyrequest=new volleyhandler<string> () {@Overridepublic void reqsuccess (String Response) {//Successful callback: Ability to manipulate returned data} @Overridepublic void Reqerror (String error) {//Failed callback: failed reminder}};
Finally, the corresponding data type needs to be requested through the Volleyhttprequest call:
Volleyhttprequest.string_request (URL, volleyrequest);
5.4 Picture request: Imageloader
Note: Request a large number of images suggested using Imageloader, request a single picture using Imageloader and imagerequest can be;
Using Imageloader, you need to use Imagelistener and do not need to use volleyhandler callbacks.
First Look at Imagelistener:
The first parameter is ImageView, the second is the default picture, and the third parameter is loading the failed picture Imagelistener Imagelistener=imageloader.getimagelistener (Imagevi EW, DEFAULTIMAGERESID, Errorimageresid)
For example: Two lines of code will be able to implement image loading and image caching (already encapsulated):
Imagelistener Imagelistener = Imageloader.getimagelistener (Imageview,r.drawable.ic_launcher, R.drawable.ic_ Launcher); Network request:
6. Optimizing the sample
To optimize the power supply list for the next article:
6.1 Optimizing Image loading
When loading images in adapter, the UI layer request picture is implemented with just two lines of code.
Implement Imagelistenerimagelistener Imagelistener = Imageloader.getimagelistener (Imageview,r.drawable.ic_launcher, R.drawable.ic_launcher); Request Picture settings picture Volleyhttprequest.image_loader (URL, imagelistener);
6.2 Optimizing Request Data
The use of jsonobject_request;
String Url=volleyhttppath.getsharedall (); Volleyhandler<jsonobject> volleyrequest=new volleyhandler<jsonobject> () {@Overridepublic void Reqsuccess (jsonobject response) {//successful parsing list<shared> list=new arraylist<shared> (); String str= "network error"; try {if (response.getstring ("msg"). Equals ("Success") &&response.getint ("code") ==1) {// JSON resolves to list<shared> jsonarray array=new Jsonarray (response.getstring ("Data")); for (int i=0;i<array.length ( ) (i++) {jsonobject object= (jsonobject) array.get (i); Gkfx S=new shared (); S.setid (Object.getint ("id")); S.setname ( Object.getstring ("name")), S.setpic (Object.getstring ("pic")), S.settotol (object.getstring ("Totol")); List.add (s);} if (List.size () >0) {//parse complete. Fit Listviewmovielistadapter adapter=new Movielistadapter (list); Move_list.setadapter (adapter);} Else{str= "did not parse out" +response.tostring (); Show (str);}} Else{show (response.getstring ("MSG"));}} catch (Jsonexception e) {//Occurrence exception Show (E.getmessage ());}} @Overridepublic void Reqerror (String erroR) {//Fail show ("Jsonobject error:" +error);}};/ /network request Volleyhttprequest.jsonobject_request (URL, volleyrequest);
6.3 Demo Download
Example--movie list optimized version download
7. Summary
Using Volley network communication framework, mainly using it to load data, loading picture mechanism, image caching mechanism, and activity linkage mechanism of implementation; not useful with image upload; The following will learn to use asynchttpclient to achieve image upload, of course, an open source framework, Please pay attention to;
Android-volley Network Communication Framework (two package data request and picture requests (including processing request queue and picture cache))