Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在改装多部分请求中发送对象数组_Android_Kotlin_Retrofit_Rx Java - Fatal编程技术网

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

Android 如何在改装多部分请求中发送对象数组,android,kotlin,retrofit,rx-java,Android,Kotlin,Retrofit,Rx Java,我想发送包含多部分数据的数组对象。我尝试了很多方法,但都不管用。我的contributor参数有问题。服务器说。列表中的其余项目需要contributor.0.id和contributor.0.role,依此类推。服务器读取到有一个contributor数组,其中包含项,但由于某种原因,他无法提取该数组 需要帮忙吗 @Multipart @POST("project/create") fun createProject( @Header("Authorization") token: S

我想发送包含多部分数据的数组对象。我尝试了很多方法,但都不管用。我的contributor参数有问题。服务器说。列表中的其余项目需要contributor.0.id和contributor.0.role,依此类推。服务器读取到有一个contributor数组,其中包含项,但由于某种原因,他无法提取该数组

需要帮忙吗

@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>>
}使用@Field

@Field("contributors[]") contributors: MutableList<Contributor>
@字段(“贡献者[]”)贡献者:可变列表
您可以使用@partMap

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

这是唯一适合我的方法

首先创建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

    }
val contributorsMap:HashMap=HashMap()
对于contributorList.withIndex()中的((索引,参与者){
contributorsMap[“contributors[${index}][id]”]=“contributor.id}”
contributorsMap[“contributors[${index}][role]”]=contributor.role
}
然后将我的函数参数改为@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>>
@Multipart
@发布(“项目/创建”)
趣味创意项目(
@标头(“授权”)标记:字符串,
@部分(“标题”)标题:字符串,
@部分img:多部分主体。部分,
@部分(“发布日期”)发布日期:字符串,
@PartMap贡献者:HashMap,
):单身

上面说@Field参数只能与表单编码一起使用。按照这个答案,您将得到结果。我做到了,这对我很有用。查一下wasi Sadam的答案
@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>>