Volley Introduction
In the usual development process, our application almost always in the network, and the network programming under Android is generally based on the HTTP protocol, the common is the HttpURLConnection and httpclient two classes. Because of the use of more, it is easy to write some repetitive code, so there are some better network framework, such as Asynchttpclient, Xutils,universal-image-loader (network picture loading framework) and so on.
Volley is a new network framework that Google exited at the 2013 I/O conference, with volley ideal for network operations where data is small but frequently communicated. It's better to download or use Xutils.
Volley jar Package Download
Given the difficulty of the domestic Sisu network, you can download it on GitHub, Https://github.com/mcxiaoke/android-volley an unofficial package that is kept in sync with the official
Import the jar package into the Lib directory, and the preparation is done.
Volley use
Stringrequest
This class can be used to fetch a string from the server
Stringrequest stringrequest =NewStringrequest ("http://www.baidu.com", NewResponse.listener<string>() {@Override Public voidOnresponse (String response) {LOG.D ("TAG", response); } }, NewResponse.errorlistener () {@Override Public voidonerrorresponse (volleyerror error) {LOG.E ("TAG", Error.getmessage (), error); } });
Applicationcontroller.getinstance (). Addtorequestqueue (Stringrequest);
Add Network Permissions
<uses-permission android:name= "Android.permission.INTERNET"/>
This code, is the Baidu home page of the HTML code to the request came over.
Requestqueue initialization Process
Public requestqueue Getrequestqueue () { // Lazy Initialize the request queue, the queue instance'll Be // created when it's accessed for the first time ifnull c12>) { = volley.newrequestqueue (Getapplicationcontext ()); } return mrequestqueue; }
The use of httpurlconnection after android2.3 before the httpclient,2.3 is also reflected here
Part of the source code in Newrequestqueue (context context, Httpstack stack):
if (Build.VERSION.SDK_INT >= 9) { new hurlstack (); Else { // Prior to Gingerbread, httpurlconnection is unreliable. // http://android-developers.blogspot.com/2011/09/androids-http-clients.html New Httpclientstack (androidhttpclient.newinstance (useragent)); }
Android Network Framework Volley