Skip to content Skip to sidebar Skip to footer

Put Request For Json With Json Array Inside

I'm trying to update a PUT request which has a JSONArray inside it and I'm constantly getting a 500 error response. Here how the api structure is: { 'imei': 514515854152463,

Solution 1:

Finally figured it out, sending the PUT request in the form of api. FYI: If you have multiple json objects inside the array, just use a for loop, here is the working answer:

privatevoidPutJsonRequest() {
        JSONArray jsonArray=newJSONArray();
        JSONObject jsonObj=newJSONObject();
        JSONObject jsonObj1=newJSONObject();
        try {

            jsonObj.put("codHawb", "02305767706");
            jsonObj.put("dataHoraBaixa", "2020-12-03T15:26:22");
            jsonObj.put("foraAlvo", "100");
            jsonObj.put("latitude", "45.222");
            jsonObj.put("longitude", " 23.23452");
            jsonObj.put("nivelBateria", "98");
            jsonObj.put("tipoBaixa", "ENTREGA");
            jsonArray.put(jsonObj);

            jsonObj1.put("imei", preferences.getIMEI());
            jsonObj1.put("franquia", preferences.getFranchise());
            jsonObj1.put("sistema", preferences.getSystem());
            jsonObj1.put("lista", preferences.getListID());
            jsonObj1.put("entregas", jsonArray);

            Log.e(TAG, "PutJsonRequest: "+jsonObj1 );

            JsonObjectRequest request = newJsonObjectRequest(Request.Method.PUT, ApiUtils.GET_LIST + preferences.getListID(),jsonObj1, newResponse.Listener<JSONObject>() {
                @OverridepublicvoidonResponse(JSONObject response) {
                    Log.e(TAG, "PUTonResponse: " + response);

                }
            }, newResponse.ErrorListener() {
                @OverridepublicvoidonErrorResponse(VolleyError error) {
                    Log.e(TAG, "PUTonResponseError: " + error);

                }
            }) {
                @OverridepublicMap<String, String> getHeaders() throws AuthFailureError {
                    HashMap<String, String> params = newHashMap<String, String>();
                    String auth1 = "Basic "
                            + Base64.encodeToString((preferences.getUserName() + ":" + preferences.getPaso()).getBytes(),
                            Base64.NO_WRAP);
                    params.put("Authorization", auth1);
                    params.put("x-versao-rt", "3.8.10");
                    params.put("x-rastreador", "ricardo");
//                    params.put("Content-Type", "application/json");
                    params.put("Content-Type", "application/json; charset=utf-8");
                    return params;
                }

                @OverridepublicStringgetBodyContentType() {
                    return"application/json; charset=utf-8";
                }
            };
            request.setTag(TAG);
            queue.add(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Comment if anyone has a doubt.

Post a Comment for "Put Request For Json With Json Array Inside"