【问题标题】:Retrofit POST nested object改造 POST 嵌套对象
【发布时间】:2021-03-12 02:36:08
【问题描述】:

我想问一个关于在 Kotlin 中使用 Retrofit 2 POST 的问题

这是 Postman 所做的输入

"assets": {
       "product": [
            {
                "eTag": "\"a59d9f11f3bfc643b00a96cabb8127a6\"",
                "location": "https---abc.com/abc.png",
                "filename": "abc",
                "type": "PD"
            },
            {
                "eTag": "\"dd74783c22bdb8c7cf5ff0185297ee06\"",
                "location": "https---abc.com/abc.png",
                "filename": "abc",
                "type": "PD"
            }
       ],
       "barcode": [
           {
                "eTag": "\"3a4a0719d5fb9f5a1e4b6b9d246db4de\"",
                "location": "https---abc.com/abc.png",
                "filename": "abc",
                "type": "BC"
            }
       ]
   }

这是来自我的界面

    @FormUrlEncoded
    @POST("create/newassets")
    fun newAssets(
        @Field("assets") assets: Asset? = null,
    ): Call<NewSkuResponse>

这是来自我的班级资产


data class Asset(

    @field:SerializedName("product")
    var product: MutableList<DataItemAsset?>,

    @field:SerializedName("barcode")
    var barcode: MutableList<DataItemAsset?>
)

我收到了 400 个错误请求,我认为我的代码不能正常工作 有什么帮助,谢谢

【问题讨论】:

  • 应用程序崩溃时得到的错误或输出是什么?

标签: kotlin retrofit2 retrofit2.6


【解决方案1】:

由 Oeganz 解决,

@Body 的作用就像一个魅力

 fun newAssets(
        @Body reqSKU: ReqSKU,
 ): Call<NewSkuResponse>
data class ReqSKU(
    @SerializedName("assets")
    val assets: Asset
)
data class Asset(

    @field:SerializedName("product")
    var product: MutableList<DataItemAsset?>,

    @field:SerializedName("barcode")
    var barcode: MutableList<DataItemAsset?>
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-27
    • 2019-02-14
    • 2021-11-16
    • 1970-01-01
    • 2018-09-23
    • 1970-01-01
    相关资源
    最近更新 更多