035 Android Volley framework network requests

1.volley introductory presentation

  Android application development time inevitably need to use network technology, and the application will send and receive network data using the HTTP protocol in most cases. Android system mainly provides two ways to perform HTTP communication, HttpURLConnection and HttpClient, almost any item in the code, we can see these two categories figure, the utilization rate is very high.

  Volley but that is the advantage AsyncHttpClient and Universal-Image-Loader is set in a suit, either as AsyncHttpClient as very simple HTTP communication, can also like Universal-Image-Loader is as easy to load images on the web. In addition to ease of use, Volley in performance has also been a significant adjustment, its design goal is very suitable to carry out the amount of data, but frequent communications network operations, and for the large amount of data network operations, such as said download files, Volley performance will be very bad.

Shown below these applications it belongs to the amount of data, but frequent network communication , and therefore very suitable for use Volley .


2.volley use environment configuration

(1) adding volley dependencies

github address: https: //github.com/mcxiaoke/android-volley

implementation 'com.mcxiaoke.volley:library:1.0.19'

(2) Statement network permissions

// network permissions, when disabled and can not be retrieved, and other related business
 < uses-permission Android: name = "android.permission.INTERNET"  />

3.StringRequest usage

(1) obtaining a target RequestQueue

// acquisition request queue 
RequestQueue requestQueue = Volley.newRequestQueue (getActivity () );

  RequestQueue is a request queue object that can cache all HTTP requests, and according to certain algorithms These requests concurrently. The interior design is very appropriate RequestQueue high concurrency, so we do not have to create a RequestQueue objects for every HTTP request, which is a huge waste of resources, basically create a RequestQueue object every need Activity and interactive network would be sufficient a.
(2) create a StringRequest objects

(3) Add this StringRequest object to the RequestQueue inside it

// the request added to the queue 
requestQueue.add (request);

 

Guess you like

Origin www.cnblogs.com/luckyplj/p/10932393.html