Android网络请求Volley
我们平时工作中用到的网络请求无非是HttpClient或者是HttpURLConnection,前者目前已经被完全废弃了,所以Volley网络框架里会有判断版本,与其自己写网络请求接口,不如用Google提供的Volley框架,方便的多,Volley既可以进行普通的网络请求,也可以请求网络图片,今天我先只分享普通的网络请求,网络图标后面再说,先从网上下载Volley源码,然后引入到工程中,这个不说了,我们进行网络请求分为两种,get和post,先看普通的get请求:
RequestQueue mQueue = Volley.newRequestQueue(MainActivity.this); StringRequest stringRequest = new StringRequest("后台地址", new Response.Listener<String>() { @Override public void onResponse(String response) { Log.e("fuqiang",response.toString()); } @Override public void onResponseHeaders(Map<String, String> headers) { } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }){ @Override public Map<String, String> getHeaders() throws AuthFailureError { Map<String,String> map = new HashMap<>(); map.put("key","value"); return map; } }; mQueue.add(stringRequest);
先定义一个RequestQueue 的队列,然后定义一个StringRequest,第一个参数是后台地址,第二个参数new Response.Listener是正常返回的结果,第三个参数new Response.ErrorListener()是错误返回的结果,getHeaders()是传递消息头,onResponseHeaders是返回的消息头,最后把StringRequest加入到RequestQueue 的队列中即可,就这么简单,接下来看下Post请求,其实也就是多一个参数,然后多一个getParams返回,进行参数的传递而已,看下面代码:
StringRequest stringRequest = new StringRequest(Request.Method.POST,"后台地址", new Response.Listener<String>() { @Override public void onResponse(String response) { Log.e("fuqiang",response.toString()); } @Override public void onResponseHeaders(Map<String, String> headers) { } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }){ @Override public Map<String, String> getHeaders() throws AuthFailureError { Map<String,String> map = new HashMap<>(); map.put("key","value"); return map; } @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String,String> map = new HashMap<>(); map.put("key","value"); return map; } };
第一个参数Request.Method.POST指定网络请求方式是Post,然后getParams里可以把网络请求参数放到HashMap里,然后传递给服务端。
除了StringRequest,还有JsonObjectRequest和JsonArrayRequest,用法和StringRequest类似,只是返回的结果要么是JSONObject,要么是JSONArray,如下:
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,"后台地址", null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Log.e("fuqiang",response.toString()); } @Override public void onResponseHeaders(Map<String, String> headers) { } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }){ @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String,String> map = new HashMap<>(); map.put("key","value"); return map; } @Override public Map<String, String> getHeaders() throws AuthFailureError { Map<String,String> map = new HashMap<>(); map.put("key","value"); return map; } }; JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST,"后台地址",null,new Response.Listener<JSONArray>(){ @Override public void onResponse(JSONArray response) { } @Override public void onResponseHeaders(Map<String, String> headers) { } },new Response.ErrorListener(){ @Override public void onErrorResponse(VolleyError error) { } }){ @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String,String> map = new HashMap<>(); map.put("key","value"); return map; } @Override public Map<String, String> getHeaders() throws AuthFailureError { Map<String,String> map = new HashMap<>(); map.put("key","value"); return map; } };
记得最后要加入到队列中,好了,网络请求就说到这,后面我会分析网络图片的请求,如有问题,欢迎指正,谢谢。