Recently busy to look at the Android network aspects of things. Google in 2013 released a framework called Volley network request, I looked at the official website, incredibly in the training inside there is a tutorial. First of all, the English thing looked
Still very uncomfortable, I would like to translate, just to learn about themselves, may finally have time to study how its source code is written. Start here:
First return: send a simple request
When you use volley, your process is divided into several steps:
1. You will create a requestqueue and pass it to a request object.
2. The requestqueue will then organize some work threads (slaves?). ) To run this network request operation.
3. Write the cache and parse the returned result after the request is completed. The job of the request is to parse the original return, and volley cares about how to transfer the returned data to your main thread.
This lesson will tell you how to send a request by volley.newrequestqueue this convenient way, this method will automatically create a requestqueue for you. If you want to know how to customize a Requestqueue
Please see the volley Guide to use the second back. Let's look at how to add a request in Requestqueue and how to cancel a request
The first step. First, you have to open the Android network permissions, add Android.permission.INTERNET to your mainifest file.
The second step.
Using Newrequestqueue:
Volley provides a handy method called Volley.newrequestqueue, which can automatically create a requestqueue for you, then it uses the default parameters to start the queue, for a chestnut:
1 FinalTextView Mtextview =(TextView) Findviewbyid (r.id.text);2 ...3 4 //instantiate the Requestqueue.5Requestqueue queue = Volley.newrequestqueue ( This);6String url = "Http://www.baidu.com";7 8 //Request A string response from the provided URL.9Stringrequest stringrequest =Newstringrequest (Request.Method.GET, URL,Ten NewResponse.listener<string>() { One @Override A Public voidOnresponse (String response) { - //Display the first characters of the response string. -Mtextview.settext ("Response is:" + response.substring (0,500)); the } -},NewResponse.errorlistener () { - @Override - Public voidonerrorresponse (volleyerror error) { +Mtextview.settext ("That didn ' t work!"); - } + }); A //ADD the request to the Requestqueue. atQueue.add (stringrequest);
Then Google was wall, testing the site with Baidu. Volley does the work is to the analytic good response to the main thread. It is easy to populate the UI with the received data in the main thread, because you can easily use the handler
Change your UI, but this is quite dangerous for the other syntax of the framework, such as canceling the operation.
The third step. Send a request.
To send a request, you first construct an Add () method that adds it to the requestqueue. When you add it in, it goes through a pipe-like thing, and then it gets executed, taking what's acquired
Parse it out and pass it to the main thread. When you invoke the call () method, volley starts a cache thread and some network distribution threads. When you add a request, it is identified and filtered by the cache thread, and the process is:
If your request can be serviced by the cache (already cached), it will be parsed by the cache thread and sent to the main thread. If it is not cached, perform the following steps:
1. Add it to the queue for network requests.
2. The first available network thread receives this work from Requestqueue and executes an HTTP request.
3. Parse the request in the worker thread
4. Write the parsed data to the cache.
5, the final data sent to the main thread.
The following is a life cycle diagram of this process:
Fourth step. Cancel a request
To cancel a request, call the Cancal () method on your request object. Once the request is canceled, volley will not execute response handler. You can cancel the outgoing request in the OnStop () method.
This way you don't have to deal with a mess of response handlers problems, whether or not onsaveinstancestate () has been called or other issues.
If you want to take advantage of this feature, you need to mark each request in execution to cancel it if necessary. There is an easy way to do this: you can do it for your request object and a Tag object
Make a connection and then you can use these tokens to cancel the request. Take a chestnut: You can label all of your activity's request and then call Requestqueue.cancelall (this) in the OnStop () method.
Similarly, you can request a label for a picture in Viewpager and cancel the request at the appropriate time so that the next tab will not be suspended because the picture request for the previous tab has not been completed while sliding the label.
Here is a chestnut using string tag:
1 Public Static FinalString TAG = "MyTag";2Stringrequest stringrequest;//assume this exists.3Requestqueue Mrequestqueue;//assume this exists.4 5 //Set the tag on the request.6 Stringrequest.settag (TAG);7 8 //ADD the request to the Requestqueue.9Mrequestqueue.add (stringrequest);
1 @Override 2 protected void OnStop () {3 Super . OnStop (); 4 if NULL ) {5 mrequestqueue.cancelall (TAG); 6 }7 }
Be careful when you cancel a request. If you use response handler to push a state or start another process, you are responsible for that. Again, once canceled, response handler will not be executed.
Volley use Guide first time (from Developer.android)