PUT 请求 Json,其中包含 Json 数组

PUT Request for Json with Json array inside

我正在尝试更新其中包含 JSON 数组的 PUT 请求,但我不断收到 500 错误响应。 api 结构如下:

{
    "imei": 514515854152463,
    "franquia": "SAO",
    "sistema": "PEGASUS",
    "lista": 2055313,
    "entregas":[
        {
            "codHawb": "02305767706",
            "dataHoraBaixa": "2020-12-03T15:26:22",
            "foraAlvo": 1000,
             "latitude": 44.4545,
            "longitude": 45.545,
            "nivelBateria": 98,
            "tipoBaixa": "ENTREGA"
        }
    ]
}

我已经在 Postman 中尝试了 api 以查看其是否正常工作。但是当我在使用 Volley 的程序中尝试它时,我遇到了那个错误。我试过 volley,这里是代码:

 private void PutJsonRequest() {
        try {
            Map<String, String> postParam = new HashMap<String, String>();
            postParam.put("imei", "514515854152463");
            postParam.put("franquia", preferences.getFranchise());
            postParam.put("sistema", preferences.getSystem());
            postParam.put("lista", preferences.getListID());
            postParam.put("dataHoraBaixa", "2020-12-03T15:26:22");
            postParam.put("foraAlvo", "100");
            postParam.put("latitude", "45.545");
            postParam.put("longitude", " 45.554");
            postParam.put("nivelBateria", "98");
            postParam.put("tipoBaixa", "ENTREGA");
            JsonObjectRequest request = new JsonObjectRequest(Request.Method.PUT, ApiUtils.GET_LIST + preferences.getListID(), new JSONObject(postParam), new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.e(TAG, "PUTonResponse: " + response);

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e(TAG, "PUTonResponseError: " + error);

                }
            }) {
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    HashMap<String, String> params = new HashMap<String, String>();
                    String auth1 = "Basic "
                            + Base64.encodeToString((preferences.getUserName() + ":" + preferences.getPass()).getBytes(),
                            Base64.NO_WRAP);
                    params.put("Authorization", auth1);
                    params.put("x-ver", "3.0");
                    params.put("x-ras", "rick");
                    params.put("Content-Type", "application/json");
                    return params;
                }

                @Override
                public String getBodyContentType() {
                    return "application/json";
                }
            };
            queue.add(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Here is the result in postman:1

Here is the result in postman:2

问题是api中有一个数组,如果只有JSON对象,我已经尝试了上面的方法但是它不起作用,请帮助我知道是否任何东西都可以更新或以不同的方式完成,以便在截击或改造中发送阵列。谢谢。

//编辑:

我尝试以另一种方式发送参数,这也给出了相同的 500 错误:

 JSONArray jsonArray=new JSONArray();
        JSONObject jsonObj=new JSONObject();

        try {

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

            Map<String, String> postParam = new HashMap<String, String>();
            postParam.put("imei", "514515854152463");
            postParam.put("franquia", preferences.getFranchise());
            postParam.put("sistema", preferences.getSystem());
            postParam.put("lista", preferences.getListID());
            postParam.put("entregas",jsonArray.toString());

终于想通了,以api的形式发送PUT请求。仅供参考:如果数组中有多个 json 对象,只需使用 for 循环,这是有效的答案:

 private void PutJsonRequest() {
        JSONArray jsonArray=new JSONArray();
        JSONObject jsonObj=new JSONObject();
        JSONObject jsonObj1=new JSONObject();
        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 = new JsonObjectRequest(Request.Method.PUT, ApiUtils.GET_LIST + preferences.getListID(),jsonObj1, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.e(TAG, "PUTonResponse: " + response);

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e(TAG, "PUTonResponseError: " + error);

                }
            }) {
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    HashMap<String, String> params = new HashMap<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;
                }

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

如有任何疑问,请发表评论。