使用凌空与Android发送Json数组POST请求

时间:2018-09-01 19:15:55

标签: android arrays json android-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”:“天”,     “ ContentTypeName”:“用户”,     “ Token”:“ U2FsdGVkX13CFEM =”,     “ FilterData”:[         {“ FilterName”:“ EmailId”,“ FilterValue”:“通过用户在此处输入电子邮件以进行检查”}]

}

2 个答案:

答案 0 :(得分:1)

尝试一下:

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);

答案 1 :(得分:0)

遵循以下三个步骤应该可以使缺少此支持的旧Volley库正常工作。

  1. 准备有效载荷并发布:

    JSONArray payloadItems =新的JSONArray();

    JSONObject payloadItem1 =新的JSONObject(); //在item1上设置属性 payloadItem1.put('prop1',“ val11”);

    payloadItems.put(payloadItem1);

    JSONObject payloadItem2 =新的JSONObject(); //在item1上设置属性 payloadItem2.put('prop1',“ val12”);

    payloadItems.put(payloadItem1);

    JsonArrayRequest请求;

    request = new JsonArrayRequest(Request.Method.POST,url,payloadItems,new Response.Listener(){ @SuppressWarnings(“未选中”) @Override 公共无效onResponse(JSONArray response){ //处理响应的逻辑 } },新的Response.ErrorListener(){ @Override 公共无效onErrorResponse(VolleyError错误){ //处理错误的逻辑 } }){

    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));
     }