Skip to content Skip to sidebar Skip to footer

How To Send Photos To Server With Additional Fields In Android App?

I try to send multiple photos to server with Retrofit. I have endpoint like this: @Multipart @POST('/v1/props') Call createProp( @Header('x-auth') String t

Solution 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

publicinterfaceApiInterface {
    @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 = newArrayList<>();    
for (int i=0; i < upFileList.size(); i++){
   parts.add(prepareFilePart("my_file["+i+"]", upFileList.get(i)));
}
Map<String, RequestBody> partMap = newHashMap<>();
partMap.put("price", createPartFromString(edtPrice.getText().toString()));
partMap.put("currency", createPartFromString(edtCurrency.getText().toString()));
partMap.put("tags", createPartFromString(newGson().toJson(tagsArrayList));
Call<User> call = client.createProp(TokenUtils.getToken(this), partMap);
call.enqueue(newCallback<ModelProp>() {
    @OverridepublicvoidonResponse(retrofit.Response<ModelProp> response, Retrofit retrofit) {
        // consume response
    }

    @OverridepublicvoidonFailure(Throwable t) {
        t.printStackTrace();
    }
});

prepareFilePart method

private MultipartBody.Part prepareFilePart(String partName, Uri fileUri){

    Filefile=newFile(fileUri.getPath(););

    RequestBodyrequestBody= RequestBody.create(MediaType.parse(getContentResolver().getType(fileUri)), file);

    return MultipartBody.Part.createFormData(partName, file.getName(),requestBody);
  }

createPartFromString method

publicRequestBodycreatePartFromString(Stringstring) {
        returnRequestBody.create(MultipartBody.FORM, string);
    }

Solution 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 :

publicMap<String, RequestBody> getMap() {
        Map<String, RequestBody> partMap = newHashMap<>();
        String authorS = author.getText().toString();
        partMap.put("price", createPartFromString(price));

        // you can put more filed to partMapreturn partMap;
     }

publicRequestBodycreatePartFromString(Stringstring) {
        returnRequestBody.create(MultipartBody.FORM, string);
    }

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

Post a Comment for "How To Send Photos To Server With Additional Fields In Android App?"