Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help in POST multiple image file using retrofit?

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);
like image 568
mahsanq Avatar asked Mar 26 '14 10:03

mahsanq


People also ask

How do I post multiple pictures on retrofit?

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.


1 Answers

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

}
like image 79
Francois Avatar answered Oct 18 '22 12:10

Francois