我尝试通过翻新将多张照片发送到服务器。我有这样的端点:
@Multipart
@POST("/v1/props")
Call<ModelProp> createProp(
@Header("x-auth") String token,
@Part List<MultipartBody.Part> photo
);
但是我不知道如何通过以下方式向此POST方法添加其他信息:
int price;
String currency;
ArrayList<String> tags;
有人可以帮助我将此字段添加到经过改造的POST吗?
编辑: 数组可能包含1000多个元素
答案 0 :(得分:2)
在Retrofit 2中,您可以通过以下方式与图像一起发送额外的数据:
编辑:基于Ali Ghafari的答案,您还可以使用PartMap
public interface ApiInterface {
@Multipart
@POST("/v1/props")
Call<ModelProp> createProp(@Header("x-auth") String token,
@Part List<MultipartBody.Part> photo,
@PartMap Map<String, RequestBody> map
}
您可以像这样使用它:
List<MultipartBody.Part> parts = new ArrayList<>();
for (int i=0; i < upFileList.size(); i++){
parts.add(prepareFilePart("my_file["+i+"]", upFileList.get(i)));
}
Map<String, RequestBody> partMap = new HashMap<>();
partMap.put("price", createPartFromString(edtPrice.getText().toString()));
partMap.put("currency", createPartFromString(edtCurrency.getText().toString()));
partMap.put("tags", createPartFromString(new Gson().toJson(tagsArrayList));
Call<User> call = client.createProp(TokenUtils.getToken(this), partMap);
call.enqueue(new Callback<ModelProp>() {
@Override
public void onResponse(retrofit.Response<ModelProp> response, Retrofit retrofit) {
// consume response
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
});
prepareFilePart
方法
private MultipartBody.Part prepareFilePart(String partName, Uri fileUri){
File file = new File(fileUri.getPath(););
RequestBody requestBody = RequestBody.create(MediaType.parse(getContentResolver().getType(fileUri)), file);
return MultipartBody.Part.createFormData(partName, file.getName(),requestBody);
}
createPartFromString
方法
public RequestBody createPartFromString(String string) {
return RequestBody.create(MultipartBody.FORM, string);
}
答案 1 :(得分:0)
将界面更改为此:
@Multipart
@POST("/v1/props")
Call<ModelProp> createProp(
@Header("x-auth") String token,
@Part List<MultipartBody.Part> photo,
@PartMap Map<String, RequestBody> map //added
);
并使用这些代码获取地图并将其发送到createProp
:
public Map<String, RequestBody> getMap() {
Map<String, RequestBody> partMap = new HashMap<>();
String authorS = author.getText().toString();
partMap.put("price", createPartFromString(price));
// you can put more filed to partMap
return partMap;
}
public RequestBody createPartFromString(String string) {
return RequestBody.create(MultipartBody.FORM, string);
}
答案 2 :(得分:0)
选项1::使用多个@Part
并正常传递参数。
@Multipart
@POST("/v1/props")
Call<ModelProp> createProp(
@Header("x-auth") String token,
@Part List<MultipartBody.Part> photo,
@Part("price") int price,
@Part("currency") String currency,
@Part("tags") List<String> tags
);
选项2::使用@PartMap
并让Map
包含您的数据。
@Multipart
@POST("/v1/props")
Call<ModelProp> createProp(
@Header("x-auth") String token,
@Part List<MultipartBody.Part> photo,
@PartMap Map<String, RequestBody> dataMap
);
并创建要传递的数据图
RequestBody price = ...
RequestBody currency = ...
RequestBody tags = ...
HashMap<String, RequestBody> map = new HashMap<>();
map.put("price", description);
map.put("currency", place);
map.put("tags", time);