博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
volley JsonObjectRequest post请求
阅读量:6987 次
发布时间:2019-06-27

本文共 2979 字,大约阅读时间需要 9 分钟。

hot3.png

本文为作者原创,转载请注明出处,否则将依法追究版权

注意:使用JsonObjectRequest或继承自JsonObjectRequest类的对象提交一个post请求时,如果有参数需要提交,就必须以JSONObject的json串方式提交.否则通过重写getParams()方法的方式提交不管用,getParams()方法中提交post参数只适用于Request对象。

直接上代码:第一部分是调用,第二部分是自定义JsonObjectRequestWithCookie

private void uploadUserOp(String opType, String musicKey) throws AuthFailureError {        Log.w("LOG","invoke uploadUserOp with "+ opType+" and "+musicKey);        HashMap
 opMap = new HashMap
();        opMap.put("op", opType);        opMap.put("key", musicKey);        JSONObject opJsonObject = new JSONObject(opMap);        JsonObjectRequestWithCookie jsonObjectRequestWithCookie = new JsonObjectRequestWithCookie(Constants.USER_HISTORY_URL,opJsonObject, new Response.Listener
() {            @Override            public void onResponse(JSONObject jsonObject) {                Log.w("LOG","get response jsonObject from post user history"+jsonObject.toString());                try {                    if(jsonObject.getString("status").equals("success")){                        Log.w("LOG", "post /api/use/history/ success");                    }                    else{                        Log.w("LOG", "post /api/use/history/ failure");                    }                } catch (JSONException e) {                    e.printStackTrace();                }            }        }, new Response.ErrorListener() {            @Override            public void onErrorResponse(VolleyError volleyError) {                Log.w("LOG", "操作失败");            }        },opMap);        jsonObjectRequestWithCookie.setCookie(localCookie);        RequestManager.getRequestQueue().add(jsonObjectRequestWithCookie);    }

 

import com.android.volley.Request;import com.android.volley.Response;import com.android.volley.toolbox.JsonObjectRequest;import org.json.JSONObject;import java.util.HashMap;import java.util.Map;/** * Created by lsc on 2014/12/24. */public class JsonObjectRequestWithCookie extends JsonObjectRequest {    private Map
 mHeaders = new HashMap<>();    public JsonObjectRequestWithCookie(String url, JSONObject jsonRequest, Response.Listener
 listener, Response.ErrorListener errorListener,Map
 map) {        super(Request.Method.POST, url, jsonRequest, listener, errorListener);    }    public JsonObjectRequestWithCookie(String url, JSONObject jsonRequest, Response.Listener
 listener, Response.ErrorListener errorListener){        super(url, jsonRequest, listener, errorListener);    }    //拿到客户端发起的http请求的Header    @Override    public Map
 getHeaders() throws AuthFailureError {        return mHeaders;    }    //发送请求时,往Header中添加cookie,可以一并发送    public void setCookie(String cookie) throws AuthFailureError {        mHeaders.put("User-Agent", "Android:1.0:2009chenqc@163.com");        mHeaders.put("Cookie",cookie);    }}

 

更多类型的post解释,可参见:

转载于:https://my.oschina.net/liusicong/blog/362215

你可能感兴趣的文章
枚举算法
查看>>
April Fools Contest 2018
查看>>
关于flume配置加载(二)
查看>>
delphi 字符串string转流TStream
查看>>
Lync 小技巧-51-Lync 2013-不加域-客户端-1-下载-证书-信任链
查看>>
awk数组命令经典生产实战应用拓展
查看>>
配套自测连载(二)
查看>>
linux下set和eval的使用小案例精彩解答
查看>>
为什么很多人努力了却死一地
查看>>
开放产品开发(OPD):Archi 汉化工具下载
查看>>
VS code for python开发利器
查看>>
高性能的MySQL(1)锁和MVCC
查看>>
如何用VDP备份虚拟机
查看>>
虚拟机安装 Windows 10 9926 预览版 “准备就绪”...... 故障
查看>>
FTP服务器的防火墙通用设置规则
查看>>
遍历系统文本全文
查看>>
《人人都能看懂经济学》读书笔记
查看>>
Linux文本比较命令:diff
查看>>
Android开发实践:JNI函数签名生成器
查看>>
危机!测试工程师真的要小心了
查看>>