jsonBody数组只有一个索引

时间:2019-02-01 10:02:00

标签: android android-volley

ArrayList<String> products = new ArrayList<>();

    RequestQueue queue = Volley.newRequestQueue(PaymentActivity.this);
    String url = backendUrl+"/api/orders/createorder";

    for(PModel pModel: pModels){
        products.add(pModel.toString());
    }

    Log.e(TAG, "createOrder: products array size"+products.size());

    JSONObject jsonBody = new JSONObject();
    try {
        jsonBody.put("products", products);
        jsonBody.put("totalprice", convertFloat((float) totalCostDouble));
        jsonBody.put("productsprice", convertFloat((float) productCostDouble));
        jsonBody.put("shippingprice", convertFloat((float) shippingCostInt));
        jsonBody.put("tax", convertFloat((float) TAX));

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

    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
            url, jsonBody,
            response -> {
                Log.e(TAG, "createOrder: response"+response);
            }, error -> {alertDialog.dismiss();
        VolleyLog.e("JSONPost", "Error: " + error.getMessage());
    });

    jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(
            0,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));


    queue.add(jsonObjReq);

我正在使用齐射将数据发布到后端,问题是我有一个“产品”数组,但是当我检查mongo数据库时,即使得到3号,我也只能得到一个包含所有产品的索引在Log

2 个答案:

答案 0 :(得分:1)

尝试

JSONArray productArray = new JSONArray(products);
jsonBody.put("products", productArray.toString());

答案 1 :(得分:0)

尝试将ArrayList都强制转换为

jsonBody.put("products", (Object) products);

类似:

jsonBody.put("products", new JSONArray(products));