Why use the volley framework
Developing Android apps often involves network operations, and the Android SDK provides HttpClient and httpurlconnection two ways to handle network operations, But when the application is more complex, we need to write a lot of code to deal with a lot of things: image caching, request scheduling and so on;
The volley framework, which was created to address this, was presented at the 2013 Google I/O Conference: Making Android apps easier and faster, abstracting the details of implementations such as the underlying HTTP client, and allowing developers to focus and create restful Request. In addition, volley executes all requests asynchronously on different threads and avoids blocking the main thread.
What are the characteristics of volley?
- Automatic dispatch of network requests
- Multiple Concurrent Network Connections
- Keeping disk and memory responses consistent by using a standard HTTP caching mechanism
- Support Request Priority
- A powerful API that supports cancellation requests, which can cancel a single request or multiple
- Easy to customize
- Robustness: Easy to update UI and get data correctly
- Includes Debug and trace tools
How to use volley
- git clone Https://android.googlesource.com/platform/frameworks/volley or https://github.com/mcxiaoke/ Android-volley
Or go to maven to download Http://central.maven.org/maven2/com/mcxiaoke/volley/library/1.0.19/library-1.0.19.jar
Requestqueue and Request in the volley
RequestQueue
The request queue used to execute the request
Request
Used to construct a request object
Request对象
There are several main types:
StringRequest
The body of the response is a string
JsonArrayRequest
Sending and receiving JSON arrays
JsonObjectRequest
Sending and receiving JSON objects
ImageRequest
Send and receive image
basic use of volley
First we need to create one RequestQueue requestQueue
, then build one that we need XXRequest req
, then pass requestQueue.add(req)
; Add the request to the request queue;
Build a
Requestqueue
Requestqueue Requestqueue=volley.newrequestqueue (this);//This refers to the context
Create a
Request(Take jsonobjectrequest as an example)
Private final String url= "http:/xxxxx"//Required URL jsonobjectrequest req=new jsonobjectrequest (url,null,new Response.listener<jsonobject> () { @Override public void Onresponse (Jsonobject Response) { // Add your own response logic, } }, new Responseerror.listener () { @Override public void Onresponseerror ( Vollererror err) { //error handling L.D ("Error Message:", "error is" +error); } });
Add req to Requestqueue
When building an JsonObjectRequest
object, four parameters are required, where the second parameter represents the HTTP method, and the third and fourth are the response listener and the response error listener, respectively, to overwrite onResponse()
and onResponseError()
method; RequestQueue
Will execute the request and will respond to the callback onResponse()
method, so you need to implement your own business logic in the Onresponse () method
POST request
Http://www.2cto.com/kf/201408/324454.html
Use of Android volley framework (i)