The volley framework in Android requests the use of network data

Problem Description: httpclient and httpurlconnection in Android SDK are used to handle complex network operations, but when the application is complex, we need to write a lot of code to deal with many things: image caching, request scheduling, etc;

Solution: volley was born to solve these problems. It was proposed at Google I / O conference in 2013: make Android application network operation more convenient and faster; It abstracts the details of the implementation of the underlying HTTP client, so that developers can pay more attention to and generate restful requests. In addition, volley executes all requests asynchronously on different threads to avoid blocking the main thread.

Volley can be said to integrate the advantages of asynchttpclient and universal image loader. It can not only carry out HTTP communication like asynchttpclient, but also easily load pictures on the network like universal image loader. In addition to being simple and easy to use, volley has also made significant adjustments in performance. Its design goal is to be very suitable for network operations with small amount of data but frequent communication. For network operations with large amount of data, such as downloading files, volley's performance will be very bad

1. What are the characteristics of Volley?

Automatic scheduling of network requests

Multiple concurrent network connections

Maintain the consistency of disk and memory responses by using standard HTTP caching mechanism

Support request priority

Powerful API supporting cancellation request, which can cancel a single request or multiple requests

Easy to customize

Robustness: it is convenient to correctly update the UI and obtain data

Includes debugging and tracking tools

2. Requestqueue and request in Volley

● requestqueue is the request queue used to execute the request

● request is used to construct a request object

● request objects mainly include the following types:

a. The body of the stringrequest response is a string

b. Jsonarrayrequest sends and receives JSON arrays

c. Jsonobjectrequest sends and receives JSON objects

d. Imagerequest send and receive image

3. Steps for using volley:

(1) Create an Android project and import volley's jar package

(2) Get request method:

(3) Post request method:

By specifying the request method as request Method. Post make it a post request, and then reset the getparams method to set the request parameters. When making a post request, volley will try to call the getparams () method in the request, the parent class of stringrequest, to obtain the post parameters.

(3) How to load pictures:

The first method of loading pictures

The second method of loading pictures

In fact, the functions of loading pictures are far more than these. Using imageloader can cache pictures, filter duplicate links and avoid sending duplicate requests

The usage of imageloader can be summarized as follows

1. Create a requestqueue object.

2. Create an imageloader object.

3. Get an imagelistener object.

4. Call the get () method of imageloader to load the pictures on the network.

The third method of loading pictures

Finally, volley provides a user-defined ImageView to load images, which can be summarized as

1. Create a requestqueue object.

2. Create an imageloader object.

3. Add a networkimageview control to the layout file.

4. Get the instance of the control in the code.

5. Set the address of the picture to be loaded.

Step 1: we declare the control in the layout

Step 2: load in the program

4. Custom request

In practical applications, we often need to integrate HTTP requests with JSON, and volley just supports this method, but we need to customize the request ourselves. Here, we use Google's gson library for integration.

1. Inherit request class

2. Rewrite parsenetworkresponse to realize the conversion between JSON and entity class. Since the entity class is uncertain, the generic JSON string used below is as follows

Step 1:

Step 2: write a test entity class with two fields, one name and one age

Step 3: calling the method is the same as stringrequest. As shown below

The above is the volley framework in Android introduced by Xiaobian. I hope it can help you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>