如何在齐射中的表单数据中发送JSON数组

时间:2017-05-09 10:35:30

标签: java android android-volley

我无法使用form-data中的volley将JSON Array发送到服务器。 我的代码如下:

StringRequest sr = new StringRequest(Request.Method.POST, url,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.e("response_det", response);
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("response_det", "" + error.getMessage() + "," + error.toString());
            }
        }) {
    @Override
    protected Map<String, String> getParams() {
        Map<String, String> params = new HashMap<>();
        params.put("phone", "XXXXXXXXXX");
        params.put("booking_id[0]", "27829929");
        params.put("booking_id[1]", "37798393");
        return params;
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {

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

        params.put("Content-Type", "application/x-www-form-urlencoded");

        return params;
    }
};

我附上了邮差集合示例的截图 enter image description here

所以基本上我发送两个参数,一个是普通字符串,另一个是JSONArray。请告诉我我做错了什么。

我关注https://stackoverflow.com/a/27091088/3022836发布我的情况中唯一的差异,我有一个额外的JSON数组。

3 个答案:

答案 0 :(得分:0)

使用以下代码发送jsonArray

subsIdArraylist = pauseListAdapter.getIdList();// for example 
            quantityArray = pauseListAdapter.getQntyArray(); // for example

            JSONArray jsonArray = new JSONArray();

            if (subsIdArraylist.size() > 0) {
                for (int i = 0; i < subsIdArraylist.size(); i++) {
                    try {

                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put(AppConstants.KEY_ID, Integer.parseInt(subsIdArraylist.get(i)));
                        jsonObject.put(AppConstants.KEY_QTY, quantityArray.get(i));
                        jsonArray.put(i, jsonObject);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
    and then in your getParams method put this jsonarray <br>
@Override
protected Map<String, String> getParams() {
    Map<String, String> params = new HashMap<>();
    params.put("phone", "XXXXXXXXXX");
    params.put("booking_id", jsonArray.toString());

    return params;
}

答案 1 :(得分:0)

喜欢@Sandeep dhiman建议的内容,然后将contentType覆盖为应用程序json

public String getBodyContentType()
    {
        return "application/json";
    }

答案 2 :(得分:0)

适用于Json数组

final String mRequestBody = jsonArray.toString();

        StringRequest request = new StringRequest(Request.Method.POST, CreateChatRoomUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (!response.equals(null)) {
                    Log.e("Response", response);
                } else {
                    Log.e("Response"," Data Null");
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("error Response", "" + error);
            }
        }) {
            @Override
            public byte[] getBody() throws AuthFailureError {
                try {
                    return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
                } catch (UnsupportedEncodingException uee) {
                    VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8");
                    uee.printStackTrace();
                    return null;
                }
            }
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("Content-Type", "application/json; charset=UTF-8"); 
                return params;
            }
        };
        RequestQueue queue = Volley.newRequestQueue(context);
        queue.add(request);