Reference Links:
Http://www.codekk.com/open-source-project-analysis/detail/Android/grumoon/Volley%20%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90
Volley-demo:
Https://github.com/android-cn/android-open-project-demo/tree/master/volley-demo
1. Import Eclipse
After importing Volley-demo (. \android-open-project-demo-master\volley-demo) into Eclipse:eclipse->file->import-> Existingandroid Code into Workspace. After importing is a homeactivity, and there will be a heap of R cannot is resolved to a variable error, restart Eclipse can
2. Learning Volley-demo
2.1 Using Stringrequest
The following is the listener code for the Stringrequest send button:
Btnsend.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {if (Stringutil.isempty ( Eturl.gettext (). toString ())) {Toastutil.showtoast (getactivity (), "Please enter request Address"); return;} Before the request, cancel the previous request (cancel the request that has not yet completed) Volleyutil.getqueue (Getactivity ()). Cancelall (this); Tvresult.settext (""); Stringrequest request = new Stringrequest (Stringutil.preurl (Eturl.gettext (). toString (). Trim ()), New listener< String> () {@Overridepublic void Onresponse (String response) {Tvresult.settext (response);}}, New Errorlistener () {@ overridepublic void Onerrorresponse (Volleyerror arg0) {toastutil.showtoast (Getactivity (), Getresources (). GetString ( R.string.request_fail_text));}); /request plus tag for cancellation of request Request.settag (this); Volleyutil.getqueue (Getactivity ()). Add (Request);});
It can be seen from the code that it is primarily a new stringrequest, which is then added to the request queue Requestqueue through the Add function.
Next look at the Stringrequest constructor:
public class Stringrequest extends Request<string> {... Public stringrequest (String URL, listener<string> Listener, Errorlistener Errorlistener);.. }
Where request is an abstract class, listener is an interface. Request has a constructor function:
Public Request (int method, string URL, Response.errorlistener listener);p ublic request (string URL, Response.errorlistener listener) {This (method.deprecated_get_or_post, URL, listener); }
There are functions in listener:
Public Voidonresponse (T response);
This function is recalled when the system stringrequest processing is complete. In this demo, the system callback Onresponse, the received data is displayed on the TextView.
3. Summary
Using volley is simple, and the process can be divided into two steps:
- Create request queue Requestqueue RQ = new Volley.newrequestqueue ()
- Create a new request Xxrequest, and then add the request to the queue RQ: Rq.add (xxrequest);
Today first understand the preliminary use of volley, the next will be the volley source code for detailed analysis
Android advanced-volley-1. Using Volley