Why volley?
Android provides two HTTP libraries for developers to implement an HTTP request. One is androidhttpclient (extended from Apache httpclient) and the other is httpurlconnection. Both have advantages and disadvantages. When we develop an application, we generally write some HTTP connection classes that can process all HTTP requests, create backend threads, manage thread pools, parse response, cache response, handle abnormal status codes and SSL connections to implement parallel or serial requests. Every developer has their own way to implement these methods. Some may use asycntask to complete network requests in the background. Some may create an HTTP connection class from the UI thread through the processing program, and then perform network operations in the working thread, then use the handler to parse the HTTP Response and return it to the main thread.
But we will eventually write the same sample code again, and we will try to duplicate the wheel in our application.
For example, the following code snippet implements an HTTP request in dobackground of aysnctask. When we get response, copy the data from the input stream of httpurlconnection to the output stream, and then convert the output stream to the jsonobject we finally want. All these sample codes are repeated in our code.
HttpURLConnection urlConnection = null;try { URL url = new URL("http://www.android.com/"); urlConnection = (HttpURLConnection) url.openConnection(); InputStream in = new BufferedInputStream(urlConnection.getInputStream()); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; // Adjust if you want int bytesRead; while ((bytesRead = in.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } JSONObject resultJSON = new JSONObject(outputStream.toString());}catch (Exception e) { e.printStackTrace();} finally { urlConnection.disconnect();}
Google proposed volley to help developers handle all network operations, so that developers can concentrate on implementing the business logic after HTTP response. Therefore, fewer network code calls can help developers reduce the number of bugs.
Note that volley is not very suitable for large file operations, such as uploading/downloading. The original method is also required for these cases.
The following are some key features of Volley:
Provide faster network requests for Android
Schedule and concurrently implement all HTTP requests in background threads and manage these threads
Provides flexible methods to concurrently run your network requests
Built-in JSON parser
You can set the request priority.
Provides retry policies for certain situations, such as timeout and internal server errors.
Flexible request cancellation
Provides image memory and disk cache for batch download Processing
You can flexibly implement your own Cache
You can implement your own httpstack (process SSL connections and patch requests)
Request tracking and debugging
Interaction with activity and lifecycle (canceling all network requests at the end of activity)
Integrated volley Project
Clone the volley project from the GIT Repository:
Git clone https://android.googlesource.com/platform/frameworks/volley
Two integration methods are available.
Generate volley. jar and put it in the libs directory
Use the volley project as a library dependency
The use of Volley mainly involves connection classes requestqueue and request
Requestqueue --- schedules the Request queue and executes the request in the working thread, and then responds to the main thread
Request --- all network requests can be created using this class. It provides the main parameters required for an HTTP request, such:
Request type-Get, post, put, delete
URL
Request data (HTTP body)
Successful response listener
Error listening
Easy to use
Initialize requestqueue
Jsonobjectrequest
Stringrequest
Gsonrequest
Of course, you can also customize the request so that it can return the corresponding Java type. For example, we can use gson to customize a request.
It is similar to stringrequest and jsonobjectrequest in use.