How to add multiple images/files on a same parameter along with other textual data using retrofit?
Single image is uploading perfectly using following interface
@Multipart
@POST("/users/updateProfile/")
public void updateProfileWithImage(
@Part("user_id") TypedString first_name,
@Part ("image") TypedFile image,
Callback<WebResponse> callback);
The solution for this problem is to pass a List or Array of MultipartBody. Part objects. Retrofit and OkHttp will then build an appropriate multipart request with all files. Java arrays or lists allow you to freely add files as required.
You can use the @MultiPart Post with @PartMap as parameter
Map<String, TypedFile> files = new HashMap<String, TypedFile>();
files.put("my file number one", new TypedFile("image/jpg", new File(filename)));
files.put("my file number two", new TypedFile("image/jpg", new File(filename)));
apiInterface.updateProfileWithImage("first name here", files);
private interface ApiInterface{
@Multipart
@POST("/users/updateProfile/")
Response updateProfileWithImage(
@Part("user_id") TypedString first_name,
@PartMap Map<String,TypedFile> Files
);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With