I am a beginner. If you have any plans, I hope you can correct them ~ Volley is a communication framework launched by Google at the I/O Conference in 2003, combined with the advantages of asynchttpclient and universal-image-loader, This simplifies the use of HTTP and the magical ability to asynchronously load images. The HTTP implementation in Android mainly includes httpurlconnection and httpclient. Google's selection of the two in the blog shows that we recommend the HTTP implementation in the ginger pie (API level = 9) java httpurlconnection is used in later versions and Apache httpclient is used in earlier versions, which is also clearly reflected in the volley framework.
Obtain volley
git clone https://android.googlesource.com/platform/frameworks/volley
Compile it into a jar file and add it to libs.
Simple request (taking stringrequest as an example)The most important part of HTTP Communication should be sending requests and receiving responses. Therefore, volley's core class is requestqueue, a request queue. It is responsible for managing working threads, reading and writing caches, parsing, and distributing responses (specific operations are implemented by specific classes ), HTTP requests to be sent will first be gathered here to wait for the working thread to implement the request. Requestqueue can be regarded as an aircraft carrier with full HTTP requests, and the working thread is a catapult. Therefore, according to the plane departure steps, we can guess the simple steps of using volley for HTTP Communication: 1. obtain requestqueue (obtain an aircraft carrier, which can be self-built or commissioned by another operator, as mentioned below) 2. instantiate a request (get an airplane and you know there are many types of planes) 3. add the request to requestqueue and wait for the worker thread to send it out (lift the plane from the hangar to the departure deck, and wait for the catapult to throw it out) the plane for departure-send a GET request following the steps above, the first step is to create a request queue. The simplest method is to use volley. newrequestqueue () is a very convenient static method. By default, we implement all the things we need (network and cache, which are implemented by default in volley ), it returns a running requestqueue (equivalent to someone else helping build an aircraft carrier ). After that, we only need to set the response listening interface of the request, and add the request to this queue, so that we can wait for the response data to knock on the door. The following is the sample code in the Google document:
1 // initialize a request queue 2 requestqueue queue = volley. newrequestqueue (this); 3 string url = "http://www.google.com"; 4 5 // create a request 6 stringrequest = new stringrequest (request. method. get, URL, 7 new response. listener () {8 @ override 9 Public void onresponse (string response) {10 // here the string type response is exhausted. 11} 12}, new response. errorlistener () {13 @ override14 public void onerrorresponse (volleyerror error) {15 // What if an error occurs? Cool! And mix them here. 16} 17}); 18 // Add this request to the Request queue 19 queue. Add (stringrequest );