Android Volley framework (I), androidvolley
Use of the Android Volley framework (1)
As this part contains a lot of content, I plan to write it into several parts. This article mainly introduces the basic use of the Volley framework as a whole;
Why use the Volley framework?
Network operations are often required to develop android applications. The Android SDK providesHttpClientAndHttpUrlConnectionThe two methods are used to process network operations, but when the application is complicated, we need to write a lot of code to process a lot of things: Image caching, request scheduling, and so on;
The Volley framework was born to solve these problems. It was proposed at the Google I/O conference in 2013: Making network operations for Android applications more convenient and convenient; abstract implementation details such as the underlying Http Client, allowing developers to focus more on and generate RESTful requests. In addition, Volley asynchronously executes all requests on different threads to avoid blocking the main thread.
What are the characteristics of Volley?
- Automatically schedule network requests
- Multiple concurrent Network Connections
- Use the standard HTTP cache mechanism to ensure the disk and memory response are consistent.
- Supported Request priority
- Supports powerful APIs for canceling requests. You can cancel a single request or multiple APIs.
- Easy to customize
- Robustness: easy to correctly update the UI and obtain data
- Debugging and tracing tools
How to Use Volley
Obtain Volley:
-Git cloneHttps://android.googlesource.com/platform/frameworks/volley
Or https://github.com/mcxiaoke/android-volley
You can also download volley. jar directly.
-If you useGit cloneTo generatevoller.jar
Run the following command in the volley directory:
android update project -pant jar
Add Volley to the project:
-Setvolley.jar
Paste inlibs
Folder, right-clickvolley.jar
File, selectAdd as Library
RequestQueue and Request in Volley
It is not difficult to see the literal meaning.
RequestQueue
Request queue used to execute requests
Request
Used to construct a request object
Request object
There are mainly the following types:
StringRequest
The response body is a string
JsonArrayRequest
JSON array for sending and receiving
JsonObjectRequest
Send and receive JSON objects
ImageRequest
Send and receive images
Basic use of Volley
First, we need to createRequestQueue reqQueue
And then buildXXRequest req
, And then passreqQueue.add(req)
; Add the request to the Request queue;
Build
RequestQueue
RequestQueue requestQueue = Volley. newRequestQueue (this); // here this refers to Context
Create
Request(Take JsonObjectRequest as an example)
Private final String url = "http://www.xxx.com/person.json" // 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 error) {// handle the error Log. d ("Error Message:", "Error is" + error );}});
Add request to requestQueue
requestQueue.add(req);
In buildingJsonObjectRequest
Four parameters are required.
JsonObjectRequest req = new JsonObjectRequest (String url, Request. Method. GET (or POST), new Response. Listener (), new ResponseError. Listener ()),
The second parameter indicates the http method. The third and fourth parameters are the response listener and the response error listener, which must be overwritten respectively.onResponse()
AndonResponseError()
Method;RequestQueue
The request will be executed and the response callback will be made.onResponse()
Method, so you need to implement your own business logic in the onResponse () method;
For more information aboutVolleyFollow your blog or Weibo posts and update them later!
- Weibo: @ mingsang Android Black history,
- Mailbox: <13141459344@163.com>
- Personal Homepage: Coder: programmer & Engineering male daily,