创建用于修改POST请求的JSON正文参数

时间:2018-11-25 21:00:03

标签: java android json android-studio retrofit

我必须使用以下数据通过改型创建POST请求。我试图创建一个与示例中提供的主体类似的主体,但是在尝试了多种方式之后,我仍然遇到了Bad Request。我想知道我在做什么错?

This the data that needs to be send from the application

此字段需要发送到CreateShipmentDetails []类型的api

enter image description here

这是我要创建的示例正文

  {
   "Shipments": [
       {
         "Shipper": {
            "Name": " DEMO ACCOUNT",
            "ContactPerson": "DEMO ACCOUNT",
            "Address1": "Block C, 3A01 & 3A02,",
            "Address2": "Level 3A, Kelana Square,",
            "Address3": "No.17, Jalan SS7/26",
            "Postcode": "47301",
            "City": "Petaling Jaya",
            "State": "Selangor",
            "CountryCode": "MY",
            "Phone1": "+60378038830"
        },
        "Consignee": {
            "ContactPerson": "LWE (AU) Ltd.",
            "Address1": "Test Address 1",
            "Address2": "Test Address 2",
            "Postcode": "70000",
            "City": "JOHOR BHARU",
            "State": "SELANGOR",
            "CountryCode": "MY",
            "Phone1": "03-8888888"
        },
        "Items": [
            {
                "Description": "item1",
                "Quantity": 10,
                "UnitValue": 20.0,
                "HSCode": "30",
                "SKU": "40",
                "Url": "50"
            }
        ],
        "Packages": [
            {
                "PackageReference": "pkg1",
                "Length": 10.0,
                "Width": 20.0,
                "Height": 30.0,
                "ActualWeight": 40.0
            }
        ],
        "PackageType": "SPX",
        "WeightType": "KG",
        "ShipmentDate": "2018-07-01T00:00:00+08:00",
        "TOSMode": "MY-E-EXPRESS",
        "CurrencyCode": "MYR"
    }
]
}

这是我为CreateShipmentDetails创建的模型

public class CreateShipmentDetails {

@SerializedName("Shipper")
@Expose
private Object Shipper;
@SerializedName("Consignee")
@Expose
private Object Consignee;
@SerializedName("Items")
@Expose
private List<ItemInfo> Items;
@SerializedName("Packages")
@Expose
private List<PakageInfo> Packages;
@SerializedName("ShipmentDate")
@Expose
private Date ShipmentDate;
@SerializedName("TOSMode")
@Expose
private String TOSMode;
@SerializedName("ReferenceNo")
@Expose
private String ReferenceNo;
@SerializedName("ReferenceSource")
@Expose
private String ReferenceSource;
@SerializedName("PackageType")
@Expose
private String PackageType;
@SerializedName("WeightType")
@Expose
private String WeightType;
@SerializedName("CurrencyCode")
@Expose
private String CurrencyCode;
@SerializedName("Remarks")
@Expose
private String Remarks;
@SerializedName("HawbNo")
@Expose
private String HawbNo;



public Object getShipper() {
    return Shipper;
}

public void setShipper(Object shipper) {
    Shipper = shipper;
}

public Object getConsignee() {
    return Consignee;
}

public void setConsignee(Object consignee) {
    Consignee = consignee;
}

public List<ItemInfo> getItems() {
    return Items;
}

public void setItems(List<ItemInfo> items) {
    Items = items;
}

public List<PakageInfo> getPackages() {
    return Packages;
}

public void setPackages(List<PakageInfo> packages) {
    Packages = packages;
}

public Date getShipmentDate() {
    return ShipmentDate;
}

public void setShipmentDate(Date shipmentDate) {
    ShipmentDate = shipmentDate;
}

public String getTOSMode() {
    return TOSMode;
}

public void setTOSMode(String TOSMode) {
    this.TOSMode = TOSMode;
}

public String getReferenceNo() {
    return ReferenceNo;
}

public void setReferenceNo(String referenceNo) {
    ReferenceNo = referenceNo;
}

public String getReferenceSource() {
    return ReferenceSource;
}

public void setReferenceSource(String referenceSource) {
    ReferenceSource = referenceSource;
}

public String getPackageType() {
    return PackageType;
}

public void setPackageType(String packageType) {
    PackageType = packageType;
}

public String getWeightType() {
    return WeightType;
}

public void setWeightType(String weightType) {
    WeightType = weightType;
}

public String getCurrencyCode() {
    return CurrencyCode;
}

public void setCurrencyCode(String currencyCode) {
    CurrencyCode = currencyCode;
}

public String getRemarks() {
    return Remarks;
}

public void setRemarks(String remarks) {
    Remarks = remarks;
}

public String getHawbNo() {
    return HawbNo;
}

public void setHawbNo(String hawbNo) {
    HawbNo = hawbNo;
}
}

这是我的界面类 公共接口InterfaceAPI {

@FormUrlEncoded
@POST("Create")
Call<List> Create(@Field("Shipments") List<CreateShipmentDetails> Shipments);
}

这是我创建呼叫的地方

    Call<List> call = APIClient.getInstance().getApiInterface().Create(shipments);
            call.enqueue(new Callback<List>() {
               @Override
               public void onResponse(Call<List> call, Response<List> response) {
                   Log.e("Response", response.message());
               }

               @Override
               public void onFailure(Call<List> call, Throwable t) {
                   Log.e("Failure :", t.getMessage());

               }
           });

我尝试使用List和JsonObject,但是我一直收到错误消息。

1 个答案:

答案 0 :(得分:0)

问题出在pojo模型类中。您用于托运人和收货人的数据类型为Object,这是错误的,因为这两个密钥分别具有一个类模型。因此代码将是这样。

    public class CreateShipmentDetails {

    @SerializedName("Shipper")
    @Expose
    private Shipper shipper;
    @SerializedName("Consignee")
    @Expose
    private Consignee consignee;