volley JsonObjectRequest post

來源:互聯網
上載者:User

標籤:

注意:使用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:[email protected]");        mHeaders.put("Cookie",cookie);    }}

更多類型的post解釋,可參見:http://www.open-open.com/lib/view/open1407727047207.html

volley JsonObjectRequest post

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.