0

I am unable to send JSON Array to the server using volley in form-data. My code as follows:

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

I am attaching a screenshot of sample postman collection enter image description here

SO basically I am sending two parameters, one is a normal string and another one is a JSONArray. Please tell me what am I doing wrong.

I am following https://stackoverflow.com/a/27091088/3022836 post the only difference in my case i have an extra JSON Array.

Community
  • 1
  • 1
Kunu
  • 5,078
  • 6
  • 33
  • 61

3 Answers3

0

Use below code sending 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;
}
Sandeep dhiman
  • 1,863
  • 2
  • 17
  • 22
0

Do like what @Sandeep dhiman suggest and then override contentType to be application json

public String getBodyContentType()
    {
        return "application/json";
    }
Basil Battikhi
  • 2,638
  • 1
  • 18
  • 34
0

It Works Fine for Json Array

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);
Ramaraju
  • 604
  • 1
  • 6
  • 17