Retrofit Android Tutorial

retrofit + android

Why I chose Retrofit ?

Before using Retrofit from square, I tried volley (from Google) and AsyncTask. After using retrofit,my works gets more easy.You must read the below topics before jumping into this Tutorial.This is a beginner based project which gives an idea of retrieving from api using Retrofit.

This project is also added to my Github

Comparison  of AsyncHttp ,Volley and Retrofit

Volley is a small library compared to Retrofit ,but it is undocumented. Retrofit is developed by Square,also famous for okhttp,picasso..etc(you can find it from here).If you need volley ,then you can from Google Training  or Volley Plus from DWork.

UPDATE: Proceed to Next Retrofit Section,How to load image api using Retrofit

UPDATE2: Flip Like Flipboard using Retrofit, How to create Flipboard using Retrofit


Retrofit is a REST Client for Android and Java by Square.This library is easy learn and has more features.This is beginner friendly compared to other Networking libraries.You can GET,POST,PUT,DELETE  ..etc using this library. You can also use picasso for image loading.Read this before using Picasso or Volley.

Get rid of the Introduction and Lets star the Coding !!!

We are using Github Api for this App :

You can search github users details using this demo app 🙂


Untitled (1)

Download APK :

qr code

1) Overview

Retrofit Classes – MakeInfo

In Retrofit,we need to create 3 classes.

1) POJO (Plain Old Java Object) or Model Class – The json retrieved from the server is added to this class.

2) Interface : Now we need to create an interface for managing url calls like GET,POST..etc.This is the service class.

3) RestAdapter Class : This is RestClient Class. Gson is used in default for the retrofit.You can use setup your own converter for this purpose like jackson which will describe on later tutorials.


2) Adding Retrofit Library to Project

For Gradle :
compile 'com.squareup.retrofit:retrofit:1.9.0'

Currently,1.9.0 is the latest version.You can get updated version from here

For JAR :
          Download jar

3) Create Android Project

1) Creating New Project in Android Studio is by : File  =>  New Project and fill the descriptions and click Next.

2) Fill the minimum SDK for the project, i use 4.0+ (Retrofit  requires Android 2.3+ or Java 6)

3) Select Blank Activity and then fill out the details Activity Name and Layout Name then click Finish.

4) For Gradle : You can add Retrofit library by adding it on app =>build.gradle (in project view).

retrofit depend

For Jar : Add jar to app => libs folder and right click on the jar file and click on Add as Library.

5) Also create two packages as API and model.

6) Right Click on API and Click New => Java Class ,then  Name it as gitapi and Kind as Interface.

7) Right Click on Package model and Click New => Java Class,then Name it as gitmodel and Kind as Class

retrofit structure

Project Structure


4) Android Manifest 


<uses-permission android:name="android.permission.INTERNET"/>

Your Manifest will look like :

5) Model Class

First,we need to create POJO or Model class.The Json from server cannot be use directly in Java,so we need to use model class.

For URL structure look like this : + “search term”

For eg :

Our Json Response look like this :


json object response

This is a JSON Object. If you don’t  know the difference between Json Array and Json Object,please read this

Use jsonschema2pojo for creating the pojo easily.Do not use this for every other Json requests,sometime it gives error. I use Source type as Json and Annotation style as Gson,then click on preview.



1) Now we need url calling using our interface.

@GET(“/users/{user}”),this will call the server.where url is from after the BASE URL.The service  calling url will start with ‘/’ and {user} is the string retrieved from edittext.

@Path(“user”) String user is the string which we get from the edit text.

response from the server is then saved into the Pojo.

  7) RestAdapter

Now,this is the main need to setup the Rest Adapter and the service.

1)  API is the Base URL.

2) We need to create a RestAdapter object with Endpoint(API) and then buid().

3) Create a service for adapter with our gitapi.

4) Call the function for getting the response,Callback is used for async method.We need Callback for both success request and error handling.

5) Our parsed json will be now in POJO. You can call each by calling each item.

 String API = "";
RestAdapter restAdapter = new RestAdapter.Builder().setLogLevel(RestAdapter.LogLevel.FULL)setEndpoint(API).build(); 
 gitapi git = restAdapter.create(gitapi.class);
git.getFeed(user,new Callback<gitmodel>() {
 public void success(gitmodel gitmodel, Response response) {
tv.setText("Github Name :"+gitmodel.getName()+
"\nWebsite :"+gitmodel.getBlog()+"\nCompany Name :"+gitmodel.getCompany());
pbar.setVisibility(View.INVISIBLE); //disable progressbar
public void failure(RetrofitError error) {
 pbar.setVisibility(View.INVISIBLE); //disable progressbar

Full Code

UPDATE: Proceed to Next Retrofit Section,How to load image api using Retrofit

  • Aiman Baharum


  • Vishal Adheli

    Nice Tutorials…
    But I’m facing some minor issues… I get the error 403 Forbidden when i type basil2style in EditText and click on the button.

    Also i want to know how to debug by placing the debug point.

    Thank you

    • makeinfo14

      Vishal Adheli, From Github,”Once you go over the rate limit you will receive an error response”.You can check it on :

      Surely, i will add more details in following tutorial 🙂

  • Pingback: RxJava学习整理 | Jacks Blog()

  • Renso

    Great job, Thanks.

    • makeinfo14

      Welcome 🙂

  • Mazda Doc

    Thank you man! you save me!

    • makeinfo14


  • Nidheesh kumar.s.v

    Hi I have tried to upload a image to server using Retrofit. My previous implementation was using DefaultHttpClient. Now i want to switch to Retrofit. I have successfully done other api calls except image uploading, it shows error message “No authentication challenges found”.

    Here is my previous working code and Retrofit service class. Please help me to point out if i have missed some thing…

    public HttpResponse updateUser(String phone,String imei,String name,String status,byte[] image) {
    try {
    mHttpClient = new DefaultHttpClient();
    mHttpPut = new HttpPut(Constants.BASE_URL +”user”);
    mHttpPut.setHeader(“Accept”, “application/json”);
    mHttpPut.setHeader(“mobile-number”, phone);
    mHttpPut.setHeader(“uid”, imei);
    MultipartEntity reqEntity = new MultipartEntity(
    if(image !=null) {
    ByteArrayBody bab = new ByteArrayBody(image, name+”.png”);
    reqEntity.addPart(“user[image]”, bab);
    reqEntity.addPart(“user[name]”, new StringBody(name));
    reqEntity.addPart(“user[custom_status]”, new StringBody(status));
    mHttpResponse = mHttpClient.execute(mHttpPut);
    } catch (Exception e) {
    return mHttpResponse;

    Corresponding Retrofit service class

    void updateUserProfile(@Header(“mobile-number”) String mPhone,@Header(“uid-number”) String imei,
    @Part(“user[image]”) TypedFile mBody,@Part(“user[name]”) String mName,
    @Part(“user[custom_status]”) String mStatus, Callback response);

  • Cesario Putera

    i get 404 not found, what happen?

    • makeinfo14

      what happened exactly?where u get error?

    • Toto

      You just don’t fill the EditText right ?
      Fill (and feel) it and it’s work 😉

    • Ishan Garg

      Cesario Putera select the server location corrrectt thats the error i sure

  • Davy Jones

    good tutorial

    • makeinfo14

      thank you 🙂

  • Swapnil Bhai

    If I got code :400 that is for failure and Retrofit not unable to catch that json from server.
    How would i will dot that..please help me

    • makeinfo14

      you can simply toast that inside ,
      public void failure(RetrofitError error)

  • Pingback: Flipboard in Android using Retrofit()

  • Gustavo Adolfo Zuluaga C.

    Thanks a lot! is very useful! 🙂

    • makeinfo14

      Welcome 🙂

  • nnb nbnbn

    Error:(8, 51) error: cannot find symbol class Callback
    Error:(8, 60) error: cannot find symbol class gitmodel
    Error:(8, 26) error: cannot find symbol class Path
    Error:(7, 6) error: cannot find symbol class GET

    Error:(35, 43) error: cannot find symbol variable button
    Error:(36, 42) error: cannot find symbol variable tv
    Error:(37, 49) error: cannot find symbol variable edit
    Error:(38, 47) error: cannot find symbol variable pb
    Error:(49, 25) error: cannot find symbol method setEndpoint(String)

    Note: / ,,,,,,,,,,,,,,/ uses or overrides a deprecated API.

    Sorry, first time doing this

    • makeinfo14

      if it is first time,please import github project to your Android Studio

  • Steve Ndende

    Hi, i went through this Tuto but I don’t see where the RestAdapter is created, is it embeded in Retrofit or it’s manualy built ?

    • makeinfo14

      Restadapter is a class from Retrofit !

  • Maher Abuthraa

    Good article, I just have a few comments on naming:

    • Change gitmodel/gitapi into Gitmodel/Gitapi to match naming convention.

    • Change gitmodel into gitPojo. that what exactly is. Also lets suppose there would be many json collections, they would be converted to List via jsonschema2pojo. which is not what might been needed. so personally creating an entity i.e. gitEntity to reformat Pojo object to entity is more appropriate.


  • Pingback: Android Retrofit Images Tutorial()

  • Daniel Hauck

    Great Tutorial! Thank you!

    • makeinfo14

      Welcome 🙂

  • Adam McNeilly

    When I use the first line in my interface I get ‘@GET’ not applicable to type. Any idea what this error means? I haven’t found anything on Google.

  • Bhupinder Joshan

    I need to send data in Put request using in retrofit android.

    but problem is that,I need to apend id in url and also send parameter.{id}



    “description”:”second CHECK”;


    I created request


    public String editItem(@Path(“id”) String thePath,@Body AddItemParam itemParam,Callback callBack);

    But i got response.

    ” EditItemInterface.editItem: Must have return type or Callback as last ”

    Please give some suggestion. what i have to do.

  • Pingback: [Android] มาเชื่อมต่อ Network ด้วย Retrofit2(อย่างง่ายๆ) กันเถอะ – Oatrice()

  • Johnny TwoShoes

    What does {user} is the string retrieved from edittext mean? I need to pass in a username:
    @POST(“/users/{user}”) ?????

  • giuliocc

    Your code is not consistent with your tutorial.. Missing classes… DataBindingUtil ??? Bit of hatchet job.. pity.. you were on the right path…