在内部带有Json数组的PUT请求Json [英] PUT Request for Json with Json array inside

查看:179
本文介绍了在内部带有Json数组的PUT请求Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新其中包含JSONArray的PUT请求,并且不断收到500错误响应.此处的api结构如下:

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,
    "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的程序中尝试该操作时,遇到了该错误.我已经尝试过凌空抽空,下面是代码:

I've tried the api in Postman to see if its working and it is. But when I'm trying it in the program using Volley I'm getting that error. I've tried volley and here is the code:

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

这是邮递员的结果:1

这是邮递员中的结果:2

问题是api中有一个数组,如果只有JSON对象但它不起作用,我已经尝试过上述方法,请帮我知道是否可以更新或以其他方式完成任何操作以排球或翻新的方式发送阵列.谢谢.

The problem is there is an array in the api, I've tried the above in the way if there are only JSON object but it is not working, please help me know if anything can be updated or done in different way for sending arrays either in volley or retrofit. Thanks.

//

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

I tried sending the params in another way and this was also giving the same 500 error:

 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循环,这是有效的答案:

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:

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

如果有人有疑问,请发表评论.

Comment if anyone has a doubt.

这篇关于在内部带有Json数组的PUT请求Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆