Volley框架使用笔记

1、使用get请求时候

      StringRequest(method, url, listener, errorListener)

       第一个参数method为选择方式 Method.GET

       第二个参数url为String字符串的网络地址 

       第三个参数listener为Listener<String>(){重写onResponse(String response)方法,返回获得的String字符串}

       第四个参数errorListener为Response.ErrorListener(){重写onErrorResponse(VolleyError error)数据请求失败调用}

     JsonObjectRequest(method, url, jsonRequest, listener, errorListener)

       第一个参数method为选择方式 Method.GET /POST

       第二个参数url为String字符串的网络地址 

       第二个参数jsonRequest为post请求时候传入的json字符串

       第三个参数listener为Listener<String>(){重写onResponse(String response)方法,返回获得的String字符串}

       第四个参数errorListener为Response.ErrorListener(){重写onErrorResponse(VolleyError error)数据请求失败调用}

2、POST请求

       JsonObjectRequest(method, url, jsonRequest, listener, errorListener)

         JsonObjectRequest只需要给jsonRequest放入一个值,改Method.post

        Map<String, String> hashMap=new HashMap<String,String>();
            hashMap.put("username", "xiaowen@redbaby.com.cn");
            hashMap.put("password", "123456");
            JSONObject object=new JSONObject(hashMap);

       StringRequest(method, url, listener, errorListener)

           则需要重写getParams()方法,传入值

           protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> hashMap=new HashMap<String,String>();
                hashMap.put("username", "xiaowen@redbaby.com.cn");
                hashMap.put("password", "123456");
                return hashMap;
            }

3、请求图片

       ImageRequest(url, listener, maxWidth, maxHeight, decodeConfig, errorListener)  

       第一个参数url为String字符串的网络地址 

       第二个参数listener为Listener<Bitmap>(){重写onResponse(Bitmapresponse)方法,返回获得的String字符串}

       第三第四个参数加载图片的最大宽高(0,0为原图)

       第五个参数decodeConfig为图片的格式(Config.RGB_565)

       第六个参数errorListener为Response.ErrorListener(){重写onErrorResponse(VolleyError error)数据请求失败调用}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值