在改造kotlin中发送Body中的数据

问题描述 投票:0回答:1

我正在使用改造来调用 API,我必须像我们在 Java @Body 中发送一样在 Body 中发送数据,但不知道如何解析数据...

 {
 "appType": "EXTERNAL",
 "appDetails":{
   "os": "MAC_OSX",
   "osVersion": "1.2",
   "appVersion": "1.0",
   "deviceFamily": "MOBILE",
   "ipAddress": "192.168.5.2"
 },
 "consumerSections":[
   "Support",
   "English",
   "other"
 ],
 "engagementAttributes": [
   {
     "type": "personal",
     "personal": {
       "contacts": [{"email":"test.com","phone":"12345678"},{"email":"test2.co.il","phone":"98765430"}],
       "age": {
         "age":30.0,
         "year":1985,
         "month":7,
         "day":22
       },
       "firstname": "test",
       "lastname": "test2",
       "gender": "FEMALE",
       "company": "liveperson"
     }
   }
 ]
}

在API调用中,我如何解析它并将数据发送到服务器。 请告诉....

android kotlin retrofit2
1个回答
0
投票

如果你需要发送纯json,你可以这样做:

@Headers("Content-Type: application/json")
@POST("login")
fun getUser(@Body body: String) : Call<User>

如果你想自动转换你的 kotlin 数据类,请添加

compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'

然后,只需使用 @Body annot 设置你的对象即可。

@Headers("Content-Type: application/json")
@POST("login")
fun getUser(@Body body: YourCustomDataObject) : Call<User>

这里是一个如何将所有retrofit2和服务接口连接在一起的示例。

© www.soinside.com 2019 - 2024. All rights reserved.