gpt4 book ai didi

android - 如何在android改造中发送json数组?

转载 作者:行者123 更新时间:2023-12-04 23:46:47 25 4
gpt4 key购买 nike

我无法将 json 数组发送到服务器。当我在 postman raw 中测试时,没关系,成功返回。

postman 生;

[
{
"product_id": 2,
"name": "Umbrella",
"price": 200,
"quantity": 1,
"totalprice": 200,
"user_id": 1
},
{
"product_id": 1,
"name": "Apple",
"price": 200,
"quantity": 1,
"totalprice": 200,
"user_id": 1
}
]

API接口(interface);
@POST("example/api/order")
Call<JSONArray> postOrder(@Body JSONArray jsonArray);

购物车 Activity ;
try {
JSONArray jsonArray = new JSONArray();
for (Cart cart : cartList) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("product_id", cart.getProduct_id());
jsonObject.put("name", cart.getName());
jsonObject.put("price", cart.getPrice());
jsonObject.put("quantity", cart.getQuantity());
jsonObject.put("totalprice", cart.getTotalprice());
jsonObject.put("user_id", cart.getUser_id());
jsonArray.put(jsonObject);
}
Log.e("JSONArray", String.valueOf(jsonArray));
} catch (JSONException jse) {
jse.printStackTrace();
}

日志;
E/JSONArray: [{"product_id":1,"name":"Umbrella","price":200,"quantity":1,"totalprice":200,"user_id":1},{"product_id":2,"name":"Apple","price":89,"quantity":1,"totalprice":89,"user_id":1}]

来自服务器的错误消息;
{"values":[{"nameValuePairs":{"product_id":1,"name":"Umbrella","price":200,"quantity":1,"totalprice":200,"user_id":1}},{"nameValuePairs":{"product_id":2,"name":"Apple","price":89,"quantity":1,"totalprice":89,"user_id":1}}]}

最佳答案

您可以直接将对象数组作为参数发送。改造将处理转换。像这样更改您的接口(interface)方法:

@POST("example/api/order")
Call<JSONArray> postOrder(@Body List<Cart> cartList);

检查此 link ,你会有一个想法。

关于android - 如何在android改造中发送json数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56981910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com