Volley的使用

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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值