JsonRequest的用法与StringRequest相似。
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest("http://m.weather.com.cn/data/101010100.html", null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("TAG", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("TAG", error.getMessage(), error);
}
});
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<String, String>();
map.put("params1", "value1");
map.put("params2", "value2");
return map;
}
设置POST参数的方法除了getParams()还有一种
HashMap<String, String> hashMap=new HashMap<String,String>();
hashMap.put("channelId",channelId);
hashMap.put("account", account);
JSONObject jsonParams =new JSONObject(hashMap);
JsonObjectRequest request=new JsonObjectRequest(Request.Method.POST, packUrl, jsonParams ,vif.loadingListener(),vif.errorListener());
其实可以直接写在构造函数中,添加参数Request.Method.POST,jsonParams