问题
I try to send multiple photos to server with Retrofit. I have endpoint like this:
@Multipart
@POST("/v1/props")
Call<ModelProp> createProp(
@Header("x-auth") String token,
@Part List<MultipartBody.Part> photo
);
But I don't know how to add to this POST method additional information with:
int price;
String currency;
ArrayList<String> tags;
Can someone help me add this fields to POST with retrofit?
edit: Array may have 1000+ elements
回答1:
In Retrofit 2, You can send it extra data with the image in the following way:
Edit: Based on Ali Ghafari answer you can also use 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
}
You can use it like this:
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
method
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
method
public RequestBody createPartFromString(String string) {
return RequestBody.create(MultipartBody.FORM, string);
}
回答2:
change your interface to this:
@Multipart
@POST("/v1/props")
Call<ModelProp> createProp(
@Header("x-auth") String token,
@Part List<MultipartBody.Part> photo,
@PartMap Map<String, RequestBody> map //added
);
and use these codes to get maps and send them to 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);
}
回答3:
Option1: Use multiple @Part
and pass your arguments normally.
@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
);
Option2: Use @PartMap
and make a Map
include your data.
@Multipart
@POST("/v1/props")
Call<ModelProp> createProp(
@Header("x-auth") String token,
@Part List<MultipartBody.Part> photo,
@PartMap Map<String, RequestBody> dataMap
);
and create a map of data to pass along
RequestBody price = ...
RequestBody currency = ...
RequestBody tags = ...
HashMap<String, RequestBody> map = new HashMap<>();
map.put("price", description);
map.put("currency", place);
map.put("tags", time);
来源:https://stackoverflow.com/questions/56707264/how-to-send-photos-to-server-with-additional-fields-in-android-app