android - 如何在 Android 中使用 Volley 在 POST 请求正文中传递数组

标签 android android-volley

这里是我用于在 Volley 请求正文中发送数组的代码。但它显示意外的响应代码 500。

这是 postman 发帖请求图片

enter image description here

here i am declaring variable of array

private ArrayList<Integer> medicine_id = new ArrayList<>();
private ArrayList<Integer> code = new ArrayList<>();
private ArrayList<String> expiry_date = new ArrayList<>();
private ArrayList<String> batch_no = new ArrayList<>();
private ArrayList<Integer> qty = new ArrayList<>();
private ArrayList<Double> cost = new ArrayList<>();
private ArrayList<Double> total = new ArrayList<>();

here i am adding value in my declared variables

medicine_id.add(618);
code.add(202012011);
expiry_date.add("2021-01-09");
batch_no.add("1");
qty.add(1);
cost.add((double) 1234);
total.add((double) 2468); 

here is the code request i am using for Post Request method

try {
        RequestQueue requestQueue = Volley.newRequestQueue(SelectedPurchaseActivity.this);
        String URL = "http://khadizamedicinecorner.com/DokanPos/api/purchase/item-save";
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("purchase_no", "87676767");
        jsonBody.put("supplier", 7);
        jsonBody.put("date", "2020-12-02");
        jsonBody.put("p_type", "Cash");
        jsonBody.put("medicine_id",medicine_id);
        jsonBody.put("code", code);
        jsonBody.put("expiry_date", expiry_date);
        jsonBody.put("batch_no", batch_no);
        jsonBody.put("qty", qty);
        jsonBody.put("cost", cost);
        jsonBody.put("total", total);
        jsonBody.put("totalQty", 2);
        jsonBody.put("subTotal", 2468);
        jsonBody.put("discount", 3);
        jsonBody.put("d_type", "%");
        jsonBody.put("payable", 2394);
        jsonBody.put("paid", 3);
        jsonBody.put("due", 2391);
    
        final String requestBody = jsonBody.toString();

  

Here is the String Request to get the string response after posting data successfully

  StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new 
        Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.i("VOLLEY Response", response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("VOLLEY Error", error.toString());
            }
        }) {
            @Override
            public String getBodyContentType() {
                return "application/json; charset=utf-8";
            }
    
            @Override
            public byte[] getBody() throws AuthFailureError {
                try {
                    return requestBody == null ? null : requestBody.getBytes("utf-8");
                } catch (UnsupportedEncodingException uee) {
                    VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
                    return null;
                }
            }
    
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("Authorization", token);
                return params;
            }
    
        requestQueue.add(stringRequest);
    
    } catch (JSONException e) {
        e.printStackTrace();
}

最佳答案

这是解决方案

StringRequest stringRequest = new StringRequest(Request.Method.POST, Constant.SAVE_ITEM,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            Toast.makeText(SelectedPurchaseActivity.this,response,Toast.LENGTH_LONG).show();
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(SelectedPurchaseActivity.this,error.toString(),Toast.LENGTH_LONG).show();
                        }
                    }){
                @Override
                protected Map<String,String> getParams(){
                    Map<String,String> params = new HashMap<String, String>();
                    params.put("purchase_no", "87676767");
                    params.put("supplier", "7");
                    params.put("date", "2020-12-02");
                    params.put("p_type", "Cash");
                    params.put("medicine_id[]","618");
                    params.put("code[]", "202012011");
                    params.put("expiry_date[]", "2021-01-09");
                    params.put("batch_no[]", "1");
                    params.put("qty[]", "1");
                    params.put("cost[]", "1234");
                    params.put("total[]", "2468");
                    params.put("totalQty", "2");
                    params.put("subTotal", "2468");
                    params.put("discount", "3");
                    params.put("d_type", "%");
                    params.put("payable", "2394");
                    params.put("paid", "3");
                    params.put("due", "2391");
                    return params;
                }
    
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("Authorization", token);
                    return params;
                }
    
            };
    
            RequestQueue queue = Volley.newRequestQueue(SelectedPurchaseActivity.this);
            queue.add(stringRequest);

关于android - 如何在 Android 中使用 Volley 在 POST 请求正文中传递数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65195178/

相关文章:

android - 用于聊天应用的 RecyclerView

java - 在ArToolkit的android java SDK中查找ArMarker和相机之间的距离

php - Android 位图图像未使用 PHP API 保存到 MySQL

java - 为什么 VOLLEY 使用不同的参数检索相同的数据?

java - 什么是IndexOutOfBoundsException?我该如何解决?

安卓市场不兼容

java - 基于 Jenkins 的 Gradle 构建失败

android - Volley Post JsonObjectRequest 在使用 getHeader 和 getParams 时忽略参数

java - 尝试建立本地连接时出错

java - 我可以使用针对两个 Activity 观察到的 View 模型吗?