Android Retrofit 2 Upload file to Server Tutotial

In previous tutorial, I had been tell you how to use retrofit 2 to retrieve data from the REST API. Able to let the user upload file to the server such as image is also a necessary requirement to create a mobile application. Multipart form-data is built for uploading the file to the server transmit by name/value pair, each pair represents by part. The difference with x-www-form-unlencoded send giant string query to the server, so its unable to upload the file. In a current tutorial, I will let you know how to use android retrofit 2 upload file to the server so you can get the image from the server.

Creating a New Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “RetrofitUploadFileExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently are support more than API 15. Click Next button.
4. Choose “Empty Activity” and Click Next button
5. Lastly, press finish button.

Add new dependencies

Add dependencies for picasso and retrofit library. Picasso is to load the image in the application, so you don’t need to write huge code to load the image.

Add new permissions access

Go to your manifest file and Add the internet, access network and read external storage permissions in your application so it able to use them in your app.

Create a new xml layout for content

This layout is the content of your main layout, i add two button which is upload and pick image.

Edit mainactivity.xml layout

Go to your mainactivity layout and edit to the following source code.

Create a new Interface

Right click your package name and create a new class. After that, change your class to interface and edit to the source code below.

Create a new Modal Class

Add a new class in your package and this class is get the respond of the url.

Create a PHP file to upload

Add a new php file in your PHP IDE and it will perform upload file to the path you specify in the php code such as /image for the example below.

Create a new RetroClient class

You need to enter your server URL in the ROOT_URL field so it will able to work later. The Upload file process is performed by the server.

Edit MainActivity.class

Edit your mainactivity class, this class to perform retrofit call from the URL. Copy and paste it to your own file. Android 6.0 and above required Read External and Write External Storage permission to access.

Run Your Project

Finally, you can try the form data call in your android project, if success, it will automatically add the image in your server.

 

(Android Retrofit 2 Upload file to Server)

Source Code

 

Android

PHP

 

(Visited 4,471 times, 2 visits today)
Advertisements

Kristofer Ng

A crazy android and web developer. Passion on developing android application and exploring new technology as a hobby.

You may also like...

8 Responses

  1. Ronney Ismael says:

    Hi Kristofer, I am not able to download your code. Is this app available on github

  2. Lester says:

    I’ve been exploring for a little bit for any high quality articles or weblog posts on this kind
    of house . Exploring in Yahoo I finally stumbled upon this website.
    Reading tjis info So i’m satisfied to show that I have an incredibly
    just right uncanny feeling I discovered just what I needed.

    I most undoubtedly will make sure to don?t omit this web site and provides it a look regularly.

  3. Thanks for finally talkig about >Android Retrofit 2 Upload file to
    Server Tutotial – QuestDot <Liked it!

  4. Alycia says:

    Thank you forr any other excellent post. The place else could anybody get that kind of injformation iin such a perfect means of writing?
    I have a presentation nex week, and I’m at the look ffor such information.

  5. Chastity says:

    I was recommended this web site via my cousin. I’m not sure whether or not this put up is wriotten via him as nobody else know such specified approximatly my trouble.
    You’re amazing! Thanks!

  6. Carl says:

    Wonderful gkods from you, man. I’ve undersstand ylur stuff previous too and you’re just too magnificent.
    I actually like what you’veacquired here, certainly like what you are
    stating andd the way in which you say it. You make
    it entertaining and you still take care of to keep it sensible.
    I can not wait to read much more from you. This is really a tremendous webb site.

  7. Lolita says:

    It’s iin reality a nice and useful piece of information. I am happy that yyou simply shared this helpful info with us.
    Please stay us informed like this. Thajk you for sharing.

Leave a Reply

Your email address will not be published. Required fields are marked *

GET YOUR EBOOK FOR FREE

Unlock free ebook "Beginning Programming ALL-IN-ONE Desk Reference for DUMMIES" now!

We will send a copy to your email