Send multiple files to server using retrofit2

Fetching Comments ...
Send multiple files to server using retrofit2

Send multiple files to server using retrofit2

If you want to send multiple files,Images, Text etc then you need to send that whole data as MultipartTypedOutput. This is same as Multipart Entity in HTTP Client & Post.

If you are using Retrofit then follow this guide.

So lets start :

this is my server url where i m going to upload files :

http://192.168.1.105/learnpainless/android/file_upload/fileUpload.php
and these are parameters : image,email,website

To send files to server we need to use POST method instead of GET method. I assume that you must know about POST and GET methods and you also know about how to use Retrofit2 API. and then we will attach ”MultipartTypedOutput” to Body of our response to send over POST method.

  • First steps is to know what server will respond back when you submit data. and then make Getter Setter of that response. In this example my server will give following response as JSON Feed :
{
  "file_name":"defiance-wallpaper-1920x1080.jpg",
  "email":"test@gmail.com",
  "website":"www.learnpainless.com",
  "message":"File uploaded successfully!",
  "error":false,
  "file_path":"http://192.168.1.105/android/file_upload/uploads/defiance-wallpaper-1920x1080.jpg"
}
  • So i will make Getter Setter file according to that response.

  • So open Android Studio and create new Project , if you want to embed in old project then skip this step.

  • Now open Build.gradle file of app directory as shown in below screenshot.

  • and add Retrofit2’s compile dependency as shown below :

Send multiple files to server using retrofit2

Send multiple files to server using retrofit2

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
  • and click on sync project, and wait till android studio download all required files.
  • When build finish goto app/java/YOUR_PACKAGE_NAME/ (in this example my package name is com.learnpainless.uploadfiles ).
  • And create new class for getter-setters, i will name that file as ”ResponsePojo.class“.
  • and make getter-setters according to Server response. my getter setter will look like below :
  • Ok so now create new Java Class name it as ”SubmitAPI” and Kind will be Interface. as shown in below screenshot.

Send multiple files to server using retrofit2

  • and write below code
  • open MainActivity.java class and create String value for url of server.The URL should be end with ’/’ as per Retrofit2.
String ROOT_URL = "http://192.168.1.105/learnpainless/android/";
  • this is content of my activity_main.xml file
  • this is content of MainActivity.java
  • content of ResponsePojo.java file
  • content of SubmitAPI.java file
  • In AndroidManifest.xml file add these 3 permissions
  • Now run your app in device and try to upload images.

Send multiple files to server using retrofit2

Send multiple files to server using retrofit2

Send multiple files to server using retrofit2

Share This:

Featured Posts

post preview
post preview
post preview
post preview
post preview

JOIN SOCIAL ACCOUNTS

Related Posts

Get The Best Of All Hands Delivered To Your Inbox

Subscribe to our newsletter and stay updated.