Learn Pain Less

HomeOur TeamContact
Android
Send multiple files to server using retrofit2
Pawneshwer Gupta
Pawneshwer Gupta
October 23, 2016
1 min

Table Of Contents

01
Send multiple files to server using retrofit2
02
If you are using Retrofit then follow this guide.
03
So lets start :
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

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
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
Send multiple files to server using retrofit2

Send multiple files to server using retrofit2
Send multiple files to server using retrofit2

Subscribe to our newsletter!

We'll send you the best of our blog just once a month. We promise.

Tags

androidRetrofit 2apijson

Share


Pawneshwer Gupta

Pawneshwer Gupta

Software Developer

Pawneshwer Gupta works as a software engineer who is enthusiastic in creating efficient and innovative software solutions.

Expertise

Python
Flutter
Laravel
NodeJS

Social Media

Related Posts

How to use Text To Speech inside RecyclerView in Android
How to use Text To Speech inside RecyclerView in Android
July 27, 2020
1 min
Learn Pain Less  © 2024, All Rights Reserved.
Crafted with by Prolong Services

Quick Links

Advertise with usAbout UsContact Us

Social Media