如何在Retrofit中发布通用对象数组

huangapple go评论78阅读模式
英文:

how to post array of generic objects in retrofit

问题

{
    "items": [
        {
            "variant": {
                "id": 48,
                "quantity": "1"
            },
            "custom_form_data": {
                "features": {
                    "delivery_email": "haw@ogloba.com",
                    "delivery_mobile": "+886970639636"
                },
                "customer_info": "TEST Han Customer Info"
            }
        }
    ]
}

这是请求的调用:

items = arrayOf(
    Variant(id = 48, quantity = "1"),
    CustomFormData(features = Features("haw@ogloba.com", "+886970639636"), customerInfo = "inof")
)
英文:

I Have this JSON , needed it to post on the server , the problem how we send array of generic objects

  "items": [
            {
                "variant": {
                    "id": 48,
                    "quantity": "1"
                },
                "custom_form_data": {
                    "features": {
                        "delivery_email": "haw@ogloba.com",
                        "delivery_mobile": "+886970639636"
                    },
                    "customer_info": "TEST Han Customer Info"
                }
            }
        ],

this is the call

 items = arrayOf(
                        Variant(id = 48, quantity = "1"),
                        CustomFormData(features = Features("haw@ogloba.com", "+886970639636") ,customerInfo = "inof")
                    ),

答案1

得分: 1

定义一个适用于该格式的数据类,类似于这样:

data class Item(
   val variant: Variant,
   val custom_form_data: CustomFormData
)

然后在你的 Retrofit ApiService 接口定义中:

interface ApiService {

   @POST("url")
   fun methodName(): List<Item>

}
英文:

Define a data class for that format, something like this:

data class Item (
   val variant: Variant,
   val custom_form_data: CustomFormData
)

And then in your retrofit ApiService interface definition:

interface ApiService {

   @POST(&#39;url&#39;)
   fun methodName(): List&lt;Item&gt;

}

huangapple
  • 本文由 发表于 2020年8月23日 22:57:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63548411.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定