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)数据请求失败调用}