如何使用改型发送gson对象

xt0899hw  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(153)

这是我的购物车项目数据进入gson对象

{"cart":[{"id":"6100","productId":"47","UserRegistrationId":"44","TotalQuantity":1,"Status":"CheckOut","Price":200},{"id":"6104","productId":"154","UserRegistrationId":"44","TotalQuantity":1,"Status":"CheckOut","Price":100}]}

出现错误,无法将数据发布到服务器

D/OkHttp: {"nameValuePairs":{"cart":{"values":[{"nameValuePairs":{"id":"6100","productId":"47","UserRegistrationId":"44","TotalQuantity":1,"Status":"CheckOut","Price":200}},{"nameValuePairs":{"id":"6104","productId":"154","UserRegistrationId":"44","TotalQuantity":1,"Status":"CheckOut","Price":100}}]}}}

这是我的密码

String status = "CheckOut";

                try {

                    JSONArray cart = new JSONArray();
                    JSONObject jsonObject1 = new JSONObject();
                    for (GetCartDataModel getCartDataModel : getCartDataModelList) {
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("id", getCartDataModel.getId());
                        jsonObject.put("productId", getCartDataModel.getProductId());
                        jsonObject.put("UserRegistrationId", getCartDataModel.getUserRegistrationId());
                        jsonObject.put("TotalQuantity", getCartDataModel.getTotalQuantity());
                        jsonObject.put("Status", status);
                        jsonObject.put("Price", getCartDataModel.getPrice());
                        cart.put(jsonObject);
                    }
                    jsonObject1.put("cart", cart);
                    Log.e("cart", String.valueOf(jsonObject1));
                    Toast.makeText(mContext, String.valueOf(jsonObject1), Toast.LENGTH_SHORT).show();

                    Call<CheckOutDataModel> call = RetrofitClient.getInstance().getApi().sendArray(jsonObject1);
                    call.enqueue(new Callback<CheckOutDataModel>() {
                        @Override
                        public void onResponse(Call<CheckOutDataModel> call, Response<CheckOutDataModel> response) {
                            CheckOutDataModel checkOutDataModel = response.body();
                            Toast.makeText(mContext, checkOutDataModel.getMessage(), Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onFailure(Call<CheckOutDataModel> call, Throwable t) {

                        }
                    });

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

接口类

@POST("CheckOut")
    Call<CheckOutDataModel> sendArray(@Body JSONObject jsonObject1);

模型类别

package com.nextsuntech.kdf1.Model;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import org.json.JSONArray;

import java.util.List;

public class CheckOutDataModel {

    @SerializedName("cart")
    @Expose
    private List<CheckOutDataModel> cart = null;

    public List<CheckOutDataModel> getCart() {
        return cart;
    }

    public void setCart(List<CheckOutDataModel> cart) {
        this.cart = cart;
    }

    @SerializedName("id")
    @Expose
    private Integer id;
    @SerializedName("ProductId")
    @Expose
    private Integer productId;
    @SerializedName("UserRegistrationId")
    @Expose
    private Integer userRegistrationId;
    @SerializedName("TotalQuantity")
    @Expose
    private Integer totalQuantity;
    @SerializedName("Status")
    @Expose
    private String status;
    @SerializedName("Price")
    @Expose
    private Integer price;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getProductId() {
        return productId;
    }

    public void setProductId(Integer productId) {
        this.productId = productId;
    }

    public Integer getUserRegistrationId() {
        return userRegistrationId;
    }

    public void setUserRegistrationId(Integer userRegistrationId) {
        this.userRegistrationId = userRegistrationId;
    }

    public Integer getTotalQuantity() {
        return totalQuantity;
    }

    public void setTotalQuantity(Integer totalQuantity) {
        this.totalQuantity = totalQuantity;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

回应类别

@SerializedName("message")
    @Expose
    private String message;
    @SerializedName("autoId")
    @Expose
    private Integer autoId;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Integer getAutoId() {
        return autoId;
    }

    public void setAutoId(Integer autoId) {
        this.autoId = autoId;
    }

Postman API

h9a6wy2h

h9a6wy2h1#

不要使用JSONObject,使用请求POJO

1.使用https://www.jsonschema2pojo.org/为json请求创建POJO类
1.使用POJO类getter和setter创建请求
1.在改造中使用POJO类对象。

Example: 

 class RequestCart() {
 //..getter//..setter
 }

按以下方式使用

@POST("checkout")
 Call<CheckOutDataModel> sendArray(@Body request:  RequestCart);

相关问题