Volley的使用

来源:互联网 发布:淘宝的标品与非标品 编辑:程序博客网 时间:2024/05/17 12:01

Volley 的GET POST
* 根据不同的请求选择不同的请求对象 返回数据不确定 可用StringRequest
* StringRequest包括JsonObjectRequest和JsonArrayRequest
* StringRequest
* JsonObjetRequest
* JsonArrayRequest
* 使用时 首先建立一个请求队列 可以针对某个请求进行取消
* Volley生命周期与Activity生命周期关联 可以在Activity关闭时 关闭请求
*
* 设置Tag标签 onStop()执行取消请求

创建请求队列:

public class MyApplication extends Application {    //创建请求队列    public  static RequestQueue requestQueue;    @Override    public void onCreate() {        super.onCreate();        //实例化请求队列        requestQueue= Volley.newRequestQueue(getApplicationContext());    }    public static RequestQueue getRequestQueue(){        return requestQueue;    }}

使用Volley进行GET POST请求

public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //volley_GET();        //Array_request();        //JsonObject_request();        //VolleyPost();        jsonObj_POST();    }    /*    * 将Volley与activiy的生命周期相关联    * */    @Override    protected void onStop() {        super.onStop();        MyApplication.getRequestQueue().cancelAll("abcGet");    }    private void volley_GET() {        String url="http://hq.sinajs.cn/list=sh601006";        StringRequest stringRequest=new StringRequest(Request.Method.GET, url,                new Response.Listener<String>() {            @Override            public void onResponse(String s) {                Log.i("onResponse==",s);            }        }, new Response.ErrorListener() {            @Override            public void onErrorResponse(VolleyError volleyError) {                Log.i("onErrorResponse",volleyError+"");            }        });        stringRequest.setTag("abcGet");        MyApplication.getRequestQueue().add(stringRequest);    }    private void JsonObject_request(){        String url="http://192.168.2.45/android.php?type=json&name=zhangsan";        JsonObjectRequest objectRequest=new JsonObjectRequest(Request.Method.GET,url,null,new Response.Listener<JSONObject>() {            @Override            public void onResponse(JSONObject jsonObject) {                try {                    Log.i("JSONObject===",jsonObject.getString("id")+"  "+" "+jsonObject.getString("name")+" "+jsonObject.getString("address"));                } catch (JSONException e) {                    e.printStackTrace();                }            }        }, new Response.ErrorListener() {            @Override            public void onErrorResponse(VolleyError volleyError) {                Log.i("onErrorResponse=", ""+volleyError);            }        });        objectRequest.setTag("jsonobj");        MyApplication.getRequestQueue().add(objectRequest);    }    private void Array_request(){        String url="http://192.168.2.45/android.php?type=array";        JsonArrayRequest arrayRequest=new JsonArrayRequest(url, new Response.Listener<JSONArray>() {            @Override            public void onResponse(JSONArray jsonArray) {                Log.i("---JSONArray---", "onResponse: "+jsonArray);            }        }, new Response.ErrorListener() {            @Override            public void onErrorResponse(VolleyError volleyError) {                Log.i("---JSONArray---", "onResponse: "+volleyError);            }        });        arrayRequest.setTag("array_request");        MyApplication.getRequestQueue().add(arrayRequest);    }    private void VolleyPost(){        String url="http://192.168.2.45/android.php";        StringRequest stringRequest=new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {            @Override            public void onResponse(String s) {                Log.i("VolleyPost", s);            }        }, new Response.ErrorListener() {            @Override            public void onErrorResponse(VolleyError volleyError) {                Log.i("VolleyPost", volleyError+"");            }        }){            @Override            protected Map<String, String> getParams() throws AuthFailureError {                Map<String,String> map=new HashMap<>();                map.put("type","json");                map.put("tel","13864455353");                return map;            }        };        stringRequest.setTag("post_request");        MyApplication.getRequestQueue().add(stringRequest);    }    private void jsonObj_POST(){        String url="http://192.168.2.45/android.php";        Map<String,String> map=new HashMap<>();        map.put("type","jsonobj");        map.put("address","shandongzibo");        JSONObject jsonObject=new JSONObject(map);        JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST,url,jsonObject,new Response.Listener<JSONObject>() {            @Override            public void onResponse(JSONObject jsonObject) {                try {                    Log.i("JSONObject POST===",jsonObject.getString("id")+"  "+" "+jsonObject.getString("name")+" "+jsonObject.getString("address"));                } catch (JSONException e) {                    e.printStackTrace();                }            }        }, new Response.ErrorListener() {            @Override            public void onErrorResponse(VolleyError volleyError) {                Log.i("onErrorResponse", volleyError+"");            }        });        jsonObjectRequest.setTag("json_post");        MyApplication.getRequestQueue().add(jsonObjectRequest);    }}
0 0
原创粉丝点击