Retrofit 2 — How to Upload Multiple Files to Server

Retrofit 2 — How to Upload Multiple Files to Server

This is the example of uploading array of files using retrofit in Android.

  • This is how the service will look like
public interface ApiService {

    @POST("/event/store")
    Call<ResModel> event_store(@Body RequestBody file);
}
  • This is how the Client class look like
public class ApiClient   {
    public static final String API_BASE_URL = "api base url";

    private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

    private static Retrofit.Builder builder = new Retrofit.Builder().baseUrl(API_BASE_URL).addConverterFactory(GsonConverterFactory.create());

    public static ApiService createService(Class<ApiService> serviceClass)
    {
        Retrofit retrofit = builder.client(httpClient.build()).build();
        return retrofit.create(serviceClass);
    }
}
  • Upload like this in activity or fragment or where you want
    ApiService service = ApiClient.createService(ApiService.class);
        
    MultipartBody.Builder builder = new MultipartBody.Builder();
    builder.setType(MultipartBody.FORM);

    
    builder.addFormDataPart("event_name", "xyz");
    builder.addFormDataPart("desc", "Lorem ipsum");
    
    // Single Image
    builder.addFormDataPart("files",file1.getName(),RequestBody.create(MediaType.parse("image/*"), file1));

    // Multiple Images 
    for (int i = 0; i <filePaths.size() ; i++) {
            File file = new File(filePaths.get(i));
            RequestBody requestImage = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            builder.addFormDataPart("event_images[]", file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file));
            }


    MultipartBody requestBody = builder.build();
    Call<ResModel> call = service.event_store(requestBody);
    call.enqueue(new Callback<ResponseBody>() {
         @Override
         public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
             Toast.makeText(getBaseContext(),"All fine",Toast.LENGTH_SHORT).show();
         }

         @Override
         public void onFailure(Call<ResponseBody> call, Throwable t) {
            Toast.makeText(getBaseContext(),t.getMessage(),Toast.LENGTH_SHORT).show();
         }
     });


Note:  filePaths.size()is Arraylist of pickup Images Path.

I hope this post is useful to you. kindly share your feedback as comment here.

Thanks for reading this article.

Md. Rayhan Chowdhury

Android Engineer | Jetpack Compose | Performance Issue Tracker

3 年

Thanks a lot. U saved me

回复
Victor Okech

Software Engineer | Tech | DevOps Consultant

7 年

Thanks for your speedy response @ Mahesh Gawale let me work on this. If I find any difficulties I will share with you.

Victor Okech

Software Engineer | Tech | DevOps Consultant

7 年

Thanks. What would be the best way to save these images in database and be able to access them from the app. For instance the Status feature allows an indefinite number of images uploaded which can then be viewed by friends on their devices. I am struggling with a database schema for this.

回复
emad pirayesh

android developer at gahvare

7 年

tnx

要查看或添加评论,请登录

Mahesh Gavale的更多文章

社区洞察

其他会员也浏览了