Volley framework details, volley framework
Reference:
Http://cache.baiducontent.com/c? M = signature & p = 86759a45d5c75ae90be2963a564496 & newp = signature & user = baidu & fm = SC & query = android + Volley % BF % F2 % BC % DCAPI % CE % C4 % B5 % b5 & qid = ce8f5d4dda-260a & p1 = 2
Http://www.2cto.com/kf/201504/388742.html
Recommended: http://blog.csdn.net/richie0006/article/details/47069635
Volley combines the advantages of AsyncHttpClient and Universal-Image-Loader to implement HTTP Communication as easily as AsyncHttpClient, you can also easily attach images on the network like Universal-Image-Loader. In addition to ease of use, Volley has also made significant adjustments in terms of performance. Its design goal is to be very suitable for performing a small amount of data, but frequent network operations, for network operations that involve a large amount of data, such as file downloads, Volley performs very poorly.
To obtain a RequestQueue object, you can call the following method to obtain it:
1 RequestQueue mQueue = Volley. newRequestQueue (context );
Note that the RequestQueue obtained here is a request queue object, which can cache all HTTP requests and then send these requests concurrently according to certain algorithms.
Basically, it is enough to create a RequestQueue object in every Activity that needs to interact with the network.
Next, to send an HTTP request, we also need to create a StringRequest object, as shown below:
1 public void volleyGet(){ 2 3 StringRequest request=new StringRequest(Method.GET, "url", new Listener<String>() { 4 5 @Override 6 7 public void onResponse(String arg0) { 8 9 // TODO Auto-generated method stub10 11 }12 13 }, new Response.ErrorListener() {14 15 16 17 @Override18 19 public void onErrorResponse(VolleyError arg0) {20 21 // TODO Auto-generated method stub22 23 }24 25 });26 27 MyApplication.getHttpQueue().add(request);28 29 }30 31 32 33 public void volleyPost(String... param) {34 35 HashMap<String, String> hm = new HashMap<String, String>();36 37 hm.put("requestPurpose", "1");38 39 hm.put("username", param[0]);40 41 hm.put("userpassword", param[1]);42 43 NormalPostRequest request = new NormalPostRequest("url", new Response.Listener<JSONObject>() {44 45 @Override46 47 public void onResponse(JSONObject arg0) {48 49 // TODO Auto-generated method stub50 51 }52 53 }, new Response.ErrorListener() {54 55 @Override56 57 public void onErrorResponse(VolleyError arg0) {58 59 // TODO Auto-generated method stub60 61 }62 63 }, hm);64 65 MyApplication.getHttpQueue().add(request);66 67 }
. JsonRequest usage
After learning the basic usage of StringRequest, let's take a look at the usage of JsonRequest. Similar to StringRequest, JsonRequest also inherits from the Request class. However, because JsonRequest is an abstract class, we cannot directly create its instance, so we can only start with its subclass. JsonRequest has two direct sub-classes: JsonObjectRequest and JsonArrayRequest. You should be able to see their differences from their names, right? One is used to request a piece of JSON data, and the other is used to request a piece of JSON array.
There is basically nothing special about their usage. First, a JsonObjectRequest object is created, as shown below:
1 JsonObjectRequest jsonObjectRequest = new JsonObjectRequest("http://m.weather.com.cn/data/101010100.html", null, 2 new Response.Listener<JSONObject>() { 3 @Override 4 public void onResponse(JSONObject response) { 5 Log.d("TAG", response.toString()); 6 } 7 }, new Response.ErrorListener() { 8 @Override 9 public void onErrorResponse(VolleyError error) { 10 Log.e("TAG", error.getMessage(), error); 11 } 12 });