簡體   English   中英

使用 volley 向 android 發送 Json 數組 POST 請求

[英]Send a Json array POST request with android using volley

我正在開發應用程序,其中我使用 volley 將數據發送到服務器,現在我想將數據以 json 數組的形式發送到服務器,但不知道如何以數組的形式發送?

    Map<String, String> postParam = new HashMap<String, String>();

    postParam.put("Token", "U2FsdGVkX13CFEM=");
    postParam.put("AppDB", "day");
    postParam.put("ContentTypeName", "Users");



    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
            AppConfig.URL_REGISTER_WITHOUT_IMG, new JSONObject(postParam),
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, response.toString());
                    //     msgResponse.setText(response.toString());
                    //  hideProgressDialog();
                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            // hideProgressDialog();
        }
    }) {

        /**
         * Passing some request headers
         * */
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/json; charset=utf-8");
            return headers;
        }
    };

    jsonObjReq.setTag(TAG);
    // Adding request to request queue
    Volley.newRequestQueue(this).add(jsonObjReq);

}

現在我想在數組中發送數據期望 json { "AppDb":"day", "ContentTypeName":"Users", "Token":"U2FsdGVkX13CFEM=", "FilterData":[ {"FilterName":"EmailId", "FilterValue":"通過用戶在此處輸入電子郵件進行檢查"}]

}

嘗試這個:

JSONArray jsonArray=new JSONArray();
JSONObject jsonObj=new JSONObject();
try {
    jsonObj.put("filterName","EmailId");
    jsonObj.put("FilterValue",// user email);
} catch (JSONException e) {
    e.printStackTrace();
}
jsonArray.put(jsonObj);

將它添加到您的參數中,如下所示:

postParam.put("FilterData",jsonArray);

以下三個步驟應該使其適用於缺乏此支持的舊 Volley 庫。

  1. 准備有效載荷並發布:

    JSONArray payloadItems = new JSONArray();

    JSONObject payloadItem1=new JSONObject(); //在item1上設置屬性payloadItem1.put('prop1',"val11");

    payloadItems.put(payloadItem1);

    JSONObject payloadItem2=new JSONObject(); //在item1上設置屬性payloadItem2.put('prop1',"val12");

    payloadItems.put(payloadItem1);

    JsonArrayRequest 請求;

    request = new JsonArrayRequest(Request.Method.POST,url,payloadItems, new Response.Listener() { @SuppressWarnings("unchecked") @Override public void onResponse(JSONArray response) { //你處理響應的邏輯 } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { //你處理錯誤的邏輯 } }) {

     public Map<String, String> getHeaders() throws AuthFailureError { Map<String,String> params = new HashMap<String, String>(); /* This is very important to pass along */ params.put("Content-Type","application/json"); //other headers if any return params; } };
  2. [如果需要] 在 Volley 包中的 Class- JsonArrayRequest 中添加此構造函數(如果尚未存在)

     public JsonArrayRequest(int method, String url, JSONArray jsonArray, Listener<JSONArray> listener, ErrorListener errorListener) { super(method, url, (jsonArray == null) ? null : jsonArray.toString(), listener, errorListener);

    }

  3. [如果需要] 如果尚未實現此方法以支持來自服務器的 JSONArray 響應,請覆蓋此方法。

     @Override protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) { String responseString; JSONArray array = new JSONArray(); if (response != null) { try { responseString = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); JSONObject obj = new JSONObject(responseString); (array).put(obj); } catch (Exception ex) { } } //return array; return Response.success(array, HttpHeaderParser.parseCacheHeaders(response)); }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM