Learn Pain Less

HomeOur TeamContact
Android
Send multiple files to server using retrofit 1.9
Pawneshwer Gupta
Pawneshwer Gupta
May 21, 2016
1 min

Table Of Contents

01
upload files using Retrofit2
02
So lets start :
Send multiple files to server using retrofit 1.9

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 Retrofit2 then follow this guide.

So lets start :

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

http://192.168.1.106/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 Retrofit 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 :
  • 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 Retrofit’s compile dependency as shown below :

Send multiple files to server using retrofit 1.9
Send multiple files to server using retrofit 1.9

Send multiple files to server using retrofit 1.9
Send multiple files to server using retrofit 1.9

compile 'com.squareup.retrofit:retrofit:1.9.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 retrofit 1.9
Send multiple files to server using retrofit 1.9

  • and write below code
public interface SubmitAPI {
@POST("/file_upload/fileUpload.php")
void submitData(@Body MultipartTypedOutput attachments, Callback<ResponsePojo> response);
}
  • open MainActivity.java class and create String value for url of server.
String ROOT_URL = "http://192.168.1.106/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 retrofit 1.9
Send multiple files to server using retrofit 1.9

Send multiple files to server using retrofit 1.9
Send multiple files to server using retrofit 1.9

Send multiple files to server using retrofit 1.9
Send multiple files to server using retrofit 1.9

Subscribe to our newsletter!

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

Tags

androidRetrofitapijson

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