mcxiaoke / android-volley

DEPRECATED
4.29k stars 1.56k forks source link

Posting a JSON array not as a String #135

Open gnumarcelo opened 8 years ago

gnumarcelo commented 8 years ago

Hi, I'm trying to post a JSON object to my backend (rails) but the array param of my object is sending as String and not as a JSON array:

{ "is_delivered": true, "group_id": "569e6d287d0d6d343000000e", "orders_list": "[{\"name\":\"Mark\",\"active\":true}, {\"name\":\"Joana\",\"active\":true}]" }

Notice that my array are sending in quotes "[...]" and not as a JSON array [...], so my backend doesn't understand it as an array but as a String and it doesn't work as expected.

On the backend API side, I don't wanna ask them to change their api to support it as String and not Json, once it will drive us to a poorly api design.

Is there a way to send it as a real JSON Array? Is it a problem only with Volley lib?

        JSONObject jParams = new JSONObject();
        try {
            jParams.put("is_delivered", true);
            jParams.put("group_id", delivery.getGroupID());
            List<JSONObject> objList = new ArrayList<JSONObject>();
            JSONObject obj = new JSONObject();
            obj.put("name", "Mark");
            obj.put("active", true);
            JSONObject obj2 = new JSONObject();
            obj2.put("name", "Joana");
            obj2.put("active", true);

            objList.add(obj);
            objList.add(obj2);

            jParams.put("orders_list", objList);

        }catch (JSONException e) {
            e.printStackTrace();
        }
...

 JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST,
                AppConfig.URL_UPDATE_USERS, jParams, successCallback, errorCallback) {
AppController.getInstance().addToRequestQueue(request, TAG_REQUEST_USR);

ps.: I'm new in the android dev ps2.: I've googled it but didn't find any answer that addresses this issue.

bacy commented 8 years ago

try to use JsonArrayRequest

2016-01-20 21:54 GMT+08:00 Marcelo Vitor F. Menezes < notifications@github.com>:

Hi, I'm trying to post a JSON object to my backend (rails) but the array param of my object is sending as String and not as a JSON array:

{ "is_delivered": true, "group_id": "569e6d287d0d6d343000000e", "orders_list": "[{\"name\":\"Mark\",\"active\":true}, {\"name\":\"Joana\",\"active\":true}]" }

Notice that my array are sending in quotes "[...]" and not as a JSON array [...], so my backend doesn't understand it as an array but as a String and it doesn't work as expected.

On the backend API side, I don't wanna ask them to change their api to support it as String and not Json, once it will drive us to a poorly api design.

Is there a way to send it as a real JSON Array? Is it a problem only with Volley lib?

ps.: I'm new in the android dev ps2.: I've googled it but didn't found any answer that addresses this issue.

— Reply to this email directly or view it on GitHub https://github.com/mcxiaoke/android-volley/issues/135.