how to send List<String> with retrofit?

爱⌒轻易说出口 提交于 2020-02-04 12:58:07

问题


I'm sending a multipart request to server and this is my interface:

@Multipart
@POST("v1/group/new")
Call<MyResponse> newGroup(
        @Header("token") String token,
        @Part MultipartBody.Part photo,
        @Part("title") RequestBody subject,
        @Part("members") List<RequestBody> members);

and for sending my members in my fragment, I change my List<String> to List<RequestBody> as below:

List<RequestBody> members = new ArrayList<>();
for(int i = 0;i < membersId.size(); i++){
    members.add(RequestBody.create(MediaType.parse("text/plain"),membersId.get(i)));
}

and it's working with multiple members! but when there is a one string in my list, retrofit doesn't sends my members as a list!!! for example:

I want to send array of strings like this :

["item1","item2","item3"]

my code works for this, but when there is only one item, retrofit sends this :

"item1"

instead of ["item1"]

what is the proper way of sending array of string in multipart with retrofit?

what am I doing wrong?


回答1:


Use something like this.

@Multipart
@POST("v1/group/new")
Call<MyResponse> newGroup(
        @Header("token") String token,
        @Part MultipartBody.Part photo,
        @Part("title") RequestBody subject,
        @Part("members[]") List<RequestBody> members);

Remember you must add [] to your members param :).



来源:https://stackoverflow.com/questions/53832007/how-to-send-liststring-with-retrofit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!