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

时间:2019-11-20 08:09:00

标签: android kotlin retrofit rx-java

我想发送包含多部分数据的数组对象。我尝试了很多方法,但是没有用。我的贡献者参数问题。服务器说。对于列表中的其余项目,必须提供contributor.0.id,并且需要contributor.0.role,依此类推。 Server读取到一个供稿者数组,其中包含项目,但由于某种原因他无法提取它。Here is how it works on postman (POST/form-data) I can't do the same with retrofit.

请帮忙吗?

@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>>

班级贡献者

class Contributor : Serializable{

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

@SerializedName("role")
@Expose
var role: String = ""

}

3 个答案:

答案 0 :(得分:0)

使用@Field

@Field("contributors[]") contributors: MutableList<Contributor>

答案 1 :(得分:0)

您可以使用@partMap

@PartMap() Map<String, List<Contributor>> contibutors

答案 2 :(得分:0)

这是为我工作的唯一方法。

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

ipykernel
ipython
jupyter_client
jupyter_core
traitlets
ipython_genutils

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

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

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

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

    }