问题描述
我想发送包含多部分数据的数组对象.我尝试了很多方法,但是没有用.我的贡献者参数问题.服务器说. contributor.0.id是必需的&列表中的其余项目是必需的contributor.0.role,依此类推. Server读取到有一个供稿者数组,并且有项目,但是由于某种原因他无法提取它.
I want to send array objects with multipart data. I tried many ways but it is not working. My issue with the contributor parameter. Server says. contributor.0.id is required & contributor.0.role is required and so on for remaining items in the list. Server reads that there is a contributor array and it has items but he can't extract it for some reason.
请帮忙吗?
@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 = ""
}
推荐答案
这是为我工作的唯一方法.
Here is the only way worked for me.
首先创建了Hashmap,并以此方式映射了我的数据
First Created Hashmap and mapped my data on this way
val contributorsMap: HashMap<String, String> = HashMap()
for((index, contributor) in contributorList.withIndex()){
contributorsMap["contributors[${index}][id]"] = "${contributor.id}"
contributorsMap["contributors[${index}][role]"] = contributor.role
}
然后将我的函数参数更新为@PartMap
Then updated my function parameter to @PartMap instead
@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>>
这篇关于如何在多部分改装请求中发送对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!