Volley框架

一、先来一个GET的请求 
 
String url="http://apis.juhe.cn/mobile/get?phone=13429667914&key=335adcc4e891ba4e4be6d7534fd54c5d";
 RequestQueue=Volley.newRequestQueue(getApplicationContext());
JsonObjectRequest JsonObjectRequest=new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject jsonObject) {
	//成功后的回调
     Toast.makeText(getApplicationContext(),jsonObject.toString(),Toast.LENGTH_SHORT).show();
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError volleyError) {
	//失败回调
    }
});


 JsonObjectRequest.setTag("abcGet");//给请求设置Tag方便在activity关闭时取消请求
 RequestQueue.add(JsonObjectRequest);//把请求添加该请求队列
 
二、在来一个Post请求
String url="http://apis.juhe.cn/mobile/get?phone=13429667914&key=335adcc4e891ba4e4be6d7534fd54c5d";
 RequestQueue=Volley.newRequestQueue(getApplicationContext());
StringRequest StringRequest=new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
    @Override
    public void onResponse(String s) {

    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError volleyError) {

    }

}){
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        Map<String, String> map=new HashMap<String,String>();
        map.put("phone","xiaoming");
        map.put("key","xiaoxiao");
        return map;
    }
};


   
   StringRequest.setTag("abcGet");
RequestQueue.add(StringRequest);
三、JsonObject的Post请求
    RequestQueue=Volley.newRequestQueue(getApplicationContext());
     Map<String, String> map=new HashMap<String,String>();
     map.put("phone","xiaoming");
     map.put("key","xiaoxiao");
     JSONObject JSONObject=new JSONObject(map);
JsonObjectRequest JsonObjectRequest=new JsonObjectRequest(Request.Method.POST, url, JSONObject, new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject jsonObject) {

    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError volleyError) {

    }
});


     JsonObjectRequest.setTag("abcGet");
     RequestQueue.add(JsonObjectRequest);



 }
四、最后来个图片请求
    iv=(ImageView)findViewById(R.id.iv_imageView);
    RequestQueue RequestQueue=Volley.newRequestQueue(getApplicationContext()
    );
    String url="https://www.baidu.com/img/bdlogo.png";
    ImageRequest ImageRequest=new ImageRequest(url, new Response.Listener<Bitmap>() {
        @Override
        public void onResponse(Bitmap bitmap) {
        iv.setImageBitmap(bitmap);
        }
    }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {

        }
    });


    ImageRequest.setTag("abcGet");
    RequestQueue.add(ImageRequest);



}




                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值