volley JsonObjectRequest post请求

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

注意:使用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<String, String> opMap = new HashMap<String, String>();
        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<JSONObject>() {
            @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<String, String> mHeaders = new HashMap<>();
    public JsonObjectRequestWithCookie(String url, JSONObject jsonRequest, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener,Map<String,String> map) {
        super(Request.Method.POST, url, jsonRequest, listener, errorListener);

    }
    public JsonObjectRequestWithCookie(String url, JSONObject jsonRequest, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener){
        super(url, jsonRequest, listener, errorListener);
    }


    //拿到客户端发起的http请求的Header
    @Override
    public Map<String, String> 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解释,可参见:http://www.open-open.com/lib/view/open1407727047207.html

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值