【发布时间】:2017-08-31 18:06:37
【问题描述】:
我有一个 API,它需要一个练习列表作为输入:
exercises[0][duration]=10
exercises[0][type]=jump
exercises[1][duration]=20
exercises[1][type]=roll
在 Android 方面,我使用 Retrofit 构建了我的 API 类。
如何将我的List<Exercise> 传递给 API 方法以获取上述参数。
目前正在尝试:
@FormUrlEncoded
@POST("api/v1/patient/{id}/workout")
fun addPatientWorkout(@Path("id") id: Long,
@Field("title") title: String,
@Field("exercises[]") exercises: List<Map<String,String>>)
: Single<Response<Workout>>
但这并没有达到我的预期。而是:
exercises[]={duration:10, type=jump}&exercises[]={duration:20, type=roll}
【问题讨论】:
-
我会以 JSON 格式传递一个简单的“练习”列表,而不是作为地图。将其作为 JSON 检索后,这种格式看起来很容易处理为数组:"exercises":[{"duration"...},{...}]
-
我无法控制 API。我必须使用那种格式。
-
知道了。是@FieldMap(见下面自己的答案)