如何在改造多部分请求中发送对象数组

Ahm*_*een 5 android kotlin rx-java retrofit

我想发送带有多部分数据的数组对象。我尝试了很多方法但不起作用。我的贡献者参数问题。服务器说。列表中的其余项目需要contributor.0.id和contributor.0.role,依此类推。服务器读取到有一个贡献者数组并且它有项目,但由于某种原因他无法提取它。这是它在邮递员(POST/表单数据)上的工作原理,我不能对改造做同样的事情。

有什么帮助吗?

@Multipart
@POST("project/create")
fun createProject(
    @Header("Authorization") token: String,
    @Part("title") title: String,
    @Part img: MultipartBody.Part,
    @Part("release_date") releaseDate: String,
    @Part("contributors[]") contributors: MutableList<Contributor>
): Single<Response<String>>
Run Code Online (Sandbox Code Playgroud)

班级贡献者

class Contributor : Serializable{

@SerializedName("id")
@Expose
var id: Int = 0

@SerializedName("role")
@Expose
var role: String = ""
Run Code Online (Sandbox Code Playgroud)

}

Ahm*_*een 3

这是对我有用的唯一方法。

首先创建 Hashmap 并以此方式映射我的数据

val contributorsMap: HashMap<String, String> = HashMap()

    for((index, contributor) in contributorList.withIndex()){

        contributorsMap["contributors[${index}][id]"] = "${contributor.id}"
        contributorsMap["contributors[${index}][role]"] = contributor.role

    }
Run Code Online (Sandbox Code Playgroud)

然后将我的函数参数更新为 @PartMap

@Multipart
@POST("project/create")
fun createProject(
    @Header("Authorization") token: String,
    @Part("title") title: String,
    @Part img: MultipartBody.Part,
    @Part("release_date") releaseDate: String,
    @PartMap contributors: HashMap<String, String>,
): Single<Response<String>>
Run Code Online (Sandbox Code Playgroud)