Volley使用之JsonObjectRequest
2017-03-03 17:30
344 查看
使用Volley以Http get的方式请求JSONObject对象,我们需要使用JsonObjectRequest类的构造方法。
new JsonObjectRequest(url,jsonObject,listener)去构造一个请求request。
url:请求的地址。
jsonObject:需要传递的数据,如果是以get方式传递,则为null,如果是以post方式传递,则需要设置数据。
listener:reponse.listener()的实例。
如果是以Post方式去请求,需要设置post的数据。
在添加当前的request到队列中的时候,我们可以指定优先级,具体是要重写request的getPriority()方法。
如果想要取消当前队列中的request,可以调用cancelAll()。
new JsonObjectRequest(url,jsonObject,listener)去构造一个请求request。
url:请求的地址。
jsonObject:需要传递的数据,如果是以get方式传递,则为null,如果是以post方式传递,则需要设置数据。
listener:reponse.listener()的实例。
RequestQueue queue = Volley.newRequestQueue(this); String url = "http://m.weather.com.cn/data/101201401.html"; JsonObjectRequest objRequest = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject obj) { System.out.println("----------:" + obj); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { error.getMessage(); } }); objRequest.setTag("obj"); queue.add(objRequest);
如果是以Post方式去请求,需要设置post的数据。
Map<String,String> map=new HashMap<String,String>(); map.put("token", "AbCdEfGh123456"); JSONObject params=new JSONObject(map); RequestQueue queue = Volley.newRequestQueue(this); String url = "http://m.weather.com.cn/data/101201401.html"; JsonObjectRequest objRequest = new JsonObjectRequest(url, params, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject obj) { System.out.println("----------:" + obj); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { error.getMessage(); } });
在添加当前的request到队列中的时候,我们可以指定优先级,具体是要重写request的getPriority()方法。
JsonObjectRequest objRequest = new JsonObjectRequest(url, params, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject obj) { System.out.println("----------:" + obj); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { error.getMessage(); } }) { @Override public com.android.volley.Request.Priority getPriority() { return Priority.HIGH; } };
如果想要取消当前队列中的request,可以调用cancelAll()。
objRequest.setTag("obj"); queue.add(objRequest); //取消请求 queue.cancelAll("obj");
相关文章推荐
- Volley StringRequest和JSONObjectRequest使用几个细节
- Volley StringRequest和JSONObjectRequest使用几个细节
- Volley StringRequest和JSONObjectRequest的使用
- Volley StringRequest和JSONObjectRequest使用几个细节
- Volley使用JsonObjectRequest发送Post请求失败
- Volley使用JsonObjectRequest发送Post请求失败
- Volley StringRequest和JSONObjectRequest使用几个细节
- Volley StringRequest和JSONObjectRequest使用几个细节
- Volley StringRequest和JSONObjectRequest使用几个细节
- Volley StringRequest和JSONObjectRequest使用几个细节
- Volley使用JsonObjectRequest发送Post请求失败
- Volley StringRequest和JSONObjectRequest使用几个细节
- Volley StringRequest和JSONObjectRequest使用几个细节
- 关于Volley中JsonObjectRequest使用Post设置参数的问题
- volley JsonObjectRequest 提交参数
- Volley的三种基本用法StringRequest的Get和post用法以及JsonObjectRequest
- 初识Volley:JsonObjectRequest、StringRequest、RequestQueue
- Volley的Get、Post方式(JsonObjectRequest、StringRequest)以及Volley获取图片的3种方式
- 解决Volley中的JsonObjectRequest jsonRequest参数无法被服务端读取的问题
- Android-Volley网络通信框架(StringRequest & JsonObjectRequest)