【问题标题】:how to give model class as parameter in retrofit which contains list of objects,如何在包含对象列表的改造中将模型类作为参数,
【发布时间】:2021-08-27 19:22:29
【问题描述】:
{
        "cartItems":[
            {
                "product":"2",
                "productType":"3",
                "qty":"50",
                "unit":"BAGS"
            },
            {
                "product":"1",
                "productType":"2",
                "qty":"50",
                "unit":"BAGS"
            }
        ]
}

//这种类型的JSON必须作为参数给出,所以请任何人帮忙清除它, 提前致谢。

【问题讨论】:

    标签: java android api kotlin retrofit


    【解决方案1】:

    这比你想象的要容易。 你只需要先制作一个模型类。然后创建一个 post 方法以在您的请求中传递该模型类。

    例如,在您的情况下:

    1. 模型类:

      public class CartModel {
      
       @SerializedName("cartItems")
       ArrayList<CartItemsModel> cartItems;
      
       public ArrayList<CartItemsModel> getCartItems() {
           return cartItems;
       }
      
       public void setCartItems(ArrayList<CartItemsModel> cartItems) {
           this.cartItems = cartItems;
       }
      
       public static class CartItemsModel {
           @SerializedName("product")
           String product;
      
           @SerializedName("qty")
           int qty;
      
           @SerializedName("BAGS")
           String unit;
      
           public String getProduct() {
               return product;
           }
      
           public void setProduct(String product) {
               this.product = product;
           }
      
           public int getQty() {
               return qty;
           }
      
           public void setQty(int qty) {
               this.qty = qty;
           }
      
           public String getUnit() {
               return unit;
           }
      
           public void setUnit(String unit) {
               this.unit = unit;
           }
       }
      

      }

    2. API 接口:

       //This is just for understanding, your method can be different according to 
       //your API structure.
      
       @POST("your-api-link") // excluding the base URL
       @FormUrlEncoded
       Call<CartModel> getUserRegistration(@Body CartModel cartModel);
      

    【讨论】:

      【解决方案2】:

      有很多资源可以自动生成模型类。

      https://json2csharp.com/json-to-pojo

      https://plugins.jetbrains.com/plugin/9960-json-to-kotlin-class-jsontokotlinclass-

      这是科特林:

      data class CarItemList(
          @SerializedName("cartItems")
          val cartItems: List<CartItem>?
      )
      
      data class CartItem(
          @SerializedName("product")
          val product: String?,
          @SerializedName("productType")
          val productType: String?,
          @SerializedName("qty")
          val qty: String?,
          @SerializedName("unit")
          val unit: String?
      )
      

      【讨论】:

        猜你喜欢
        • 2020-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-02
        相关资源
        最近更新 更多