我正在尝试将String列表参数添加到多部分请求中.
使用Apache Http,我设置如下参数:
MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(mpEntity);
for (User member : members) {
mpEntity.addPart("user_ids[]", new StringBody(member.id.toString()));
}
Run Code Online (Sandbox Code Playgroud)
如何在Retrofit上执行此操作?
看一眼MultipartTypedOutput。我不认为有内置的支持,所以你必须自己构建它或编写一个Convertor.
在您的服务界面:
@POST("/url")
Response uploadUserIds(@Body MultipartTypedOutput listMultipartOutput);
Run Code Online (Sandbox Code Playgroud)
在您的来电者处:
MultipartTypedOutput mto = new MultipartTypedOutput();
for (String userId : userIds){
mto.addPart("user_ids[]", new TypedString(userId));
}
service.uploadUserIds(mto);
Run Code Online (Sandbox Code Playgroud)
这将构造类似的结果:
Content-Type: multipart/form-data; boundary=49f201f1-7379-4390-9ea5-97d74e78bb63
Content-Length: 820
--49f201f1-7379-4390-9ea5-97d74e78bb63
Content-Disposition: form-data; name="name"
Content-Type: text/plain; charset=UTF-8
Content-Length: 10
Content-Transfer-Encoding: binary
yourString
--49f201f1-7379-4390-9ea5-97d74e78bb63
Content-Disposition: form-data; name="name"
Content-Type: text/plain; charset=UTF-8
Content-Length: 10
Content-Transfer-Encoding: binary
yourString
--49f201f1-7379-4390-9ea5-97d74e78bb63
Content-Disposition: form-data; name="name"
Content-Type: text/plain; charset=UTF-8
Content-Length: 10
Content-Transfer-Encoding: binary
yourString
--49f201f1-7379-4390-9ea5-97d74e78bb63
Content-Disposition: form-data; name="name"
Content-Type: text/plain; charset=UTF-8
Content-Length: 10
Content-Transfer-Encoding: binary
yourString
--49f201f1-7379-4390-9ea5-97d74e78bb63--
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2798 次 |
| 最近记录: |