Android jsonBody数组只有一个索引 ArrayList products=new ArrayList(); RequestQueue=Volley.newRequestQueue(PaymentActivity.this); 字符串url=backendUrl+“/api/orders/createorder”; 用于(PModel PModel:pModels){ products.add(pModel.toString()); } Log.e(标记“createOrder:products数组大小”+products.size()); JSONObject jsonBody=新的JSONObject(); 试一试{ jsonBody.put(“产品”,产品); put(“totalprice”,convertFloat((float)totalCostDouble)); put(“productsprice”,convertFloat((float)productCostDouble)); jsonBody.put(“shippingprice”,convertFloat((float)shippingCostInt)); jsonBody.put(“税”,兑换浮动((浮动)税)); }捕获(JSONException e){ e、 printStackTrace(); } JsonObjectRequest JSONObjectReq=新的JsonObjectRequest(Request.Method.POST, url,jsonBody, 答复->{ Log.e(标签,“createOrder:response”+response); },错误->{alertDialog.discouse(); e(“JSONPost”,“Error:+Error.getMessage()); }); jsonObjReq.setRetryPolicy(新的DefaultRetryPolicy( 0, DefaultRetryPolicy.DEFAULT\u最大重试次数, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); add(jsonObjReq);
我正在使用截图将数据发布到我的后端,问题是我有一个“产品”数组,但当我检查我的mongo DB时,我只得到一个包含所有产品的索引,即使我在Android jsonBody数组只有一个索引 ArrayList products=new ArrayList(); RequestQueue=Volley.newRequestQueue(PaymentActivity.this); 字符串url=backendUrl+“/api/orders/createorder”; 用于(PModel PModel:pModels){ products.add(pModel.toString()); } Log.e(标记“createOrder:products数组大小”+products.size()); JSONObject jsonBody=新的JSONObject(); 试一试{ jsonBody.put(“产品”,产品); put(“totalprice”,convertFloat((float)totalCostDouble)); put(“productsprice”,convertFloat((float)productCostDouble)); jsonBody.put(“shippingprice”,convertFloat((float)shippingCostInt)); jsonBody.put(“税”,兑换浮动((浮动)税)); }捕获(JSONException e){ e、 printStackTrace(); } JsonObjectRequest JSONObjectReq=新的JsonObjectRequest(Request.Method.POST, url,jsonBody, 答复->{ Log.e(标签,“createOrder:response”+response); },错误->{alertDialog.discouse(); e(“JSONPost”,“Error:+Error.getMessage()); }); jsonObjReq.setRetryPolicy(新的DefaultRetryPolicy( 0, DefaultRetryPolicy.DEFAULT\u最大重试次数, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); add(jsonObjReq);,android,android-volley,Android,Android Volley,我正在使用截图将数据发布到我的后端,问题是我有一个“产品”数组,但当我检查我的mongo DB时,我只得到一个包含所有产品的索引,即使我在日志中得到大小为3的尝试将该数组列表转换为类似的: ArrayList<String> products = new ArrayList<>(); RequestQueue queue = Volley.newRequestQueue(PaymentActivity.this); String url = backen
日志中得到大小为3的尝试将该数组列表转换为类似的:
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);
或类似:
jsonBody.put("products", (Object) products);
试一试
这让我想知道,是否需要.toString()
,是否需要.dk,我现在使用的是改型..:p@MartinZeitlermapping by annotations确实更方便,尤其是当它变得复杂时。@SantanuSur同样的问题你能发布你期望的json模型吗?后端?还有你现在得到的@SimpleWebDesigner
jsonBody.put("products", new JSONArray(products));
JSONArray productArray = new JSONArray(products);
jsonBody.put("products", productArray.toString());