如何发送 List<String>与改造? [英] how to send List&lt;String&gt; with retrofit?

查看:65
本文介绍了如何发送 List<String>与改造?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向服务器发送多部分请求,这是我的界面:

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);

为了在我的片段中发送我的成员,我将我的 List 更改为 List 如下:

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"]

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

我的代码适用于此,但是当只有一项时,改造会发送:

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

项目1"

而不是 ["item1"]

instead of ["item1"]

通过改造在多部分中发送字符串数组的正确方法是什么?

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

我做错了什么?

推荐答案

使用类似的东西.

@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 :).

这篇关于如何发送 List<String>与改造?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆