How to use Retrofit in Android – Android Studio Tutorial

How to use Retrofit in Android – Android Studio Tutorial

android-retrofit-tutorial-mobilesiri

Retrofit Tutorial By: Muhammad Daniyal Anwar

Retrofit library is created by Square Open Source, it’s a REST client for android and java. By the use of this library, it is easy to request web services of REST with GET, POST, PUT and much more.

Now it’s time to start our coding.
Creating Android Project
Open android studio and create new project
retrofit-andriod-mobilesiri

And then select a blank activity.
Now our next step is to add Retrofit library in our project.
Open build.gradle file

retrofit-andriod-mobilesiri-1

and paste below code under dependency:

compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.squareup.okhttp:okhttp:2.4.0'

see below picture:
retrofit-android-mobilesiri-2

Now open Manifest file and add Internet Permission.

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

retrofit-android-mobilesiri-3

We need to create three classes in Retrofit:

  1. POJO or Model Class (to retrieve JSON or save data from server)
  2. Interface (for managing all network parameters)
  3. Rest Adapter Class

Now I’m going to create Model Class,
I’m using:
https://api.github.com/users/mobilesiri

and my JSON data look like this:

retrofit-android-mobilesiri-4
we need to know about JsonArray and JSON Object first.
{ } (it represent JsonObject) and
[ ] (it represent JsonArray)
Now for creating photo easily use jsonschema2pojo
retrofit-android-mobilesiri-5

 

Paste JSON data in left box and check source type JSON and Annotation style: JSON then clicks preview to see all POJO classes
retrofit-android-mobilesiri-6

copy all code and paste into your project in my case, I’m creating a class name Pojo.class and paste this
retrofit-android-mobilesiri-7

you need to add below two lines code in build.Gradle file under dependency:


compile 'com.google.code.gson:gson:2.2.+'
compile 'org.glassfish:javax.annotation:10.0-b28'

Now I’m going to create interface for all HTTP methods and parameter, see the below picture for creating interface

retrofit-android-mobilesiri-8
retrofit-android-mobilesiri-9

In my case, I have created MInterface. add below code to interface.


public interface MInterface {
@GET("/users/mobilesiri")
void getUser(Callback<Pojo>uscb);
}

Now its time to move our MainActivity, open main.xml and add below code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".model.MainActivity">
<LinearLayout

android:layout_width=“wrap_content”
android:layout_centerInParent=“true”
android:orientation=“vertical”
android:layout_height=“wrap_content”>

<TextView
android:id=“@+id/txt_comp”
android:layout_width=“wrap_content”
android:layout_margin=“10dp”
android:layout_height=“wrap_content”
android:textStyle=“bold”
android:textSize=“20dp”
android:text=“Company Name” />

<TextView
android:id=“@+id/txt_blog”
android:layout_width=“wrap_content”
android:layout_margin=“10dp”
android:layout_height=“wrap_content”
android:textStyle=“bold”
android:textSize=“20dp”
android:text=“Website” />

<TextView
android:id=“@+id/txt_hurl”
android:layout_width=“wrap_content”
android:layout_margin=“10dp”
android:textSize=“20dp”
android:layout_height=“wrap_content”
android:textStyle=“bold”
android:text=“Git URL” />

</LinearLayout>
</RelativeLayout>

Now I’m creating RestAdapter object and set base url to EndPoint and then creating object of our interface:


String url = "https://api.github.com";

RestAdapter radapter=new RestAdapter.Builder().setEndpoint(url).build();

MInterface restInt=radapter.create(MInterface.class);
Now I’m calling getUser method:
restInt.getUser(new Callback<Pojo>() {
@Override
public void success(Pojo model, Response response) {
company.setText(“Company Name :”+model.getCompany());
blog.setText(“Website :”+model.getBlog());
htmlurl.setText(“Git URL :”+model.getHtmlUrl());
}

}

Complete MainActivity.java code:

package com.mobilesiri.retrofit.model;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;

import com.mobilesiri.retrofit.R;

public class MainActivity extends Activity {

String url = “https://api.github.com”;

TextView company;
TextView blog;
TextView htmlurl;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

company = (TextView) findViewById(R.id.txt_comp);
blog = (TextView) findViewById(R.id.txt_blog);
htmlurl = (TextView) findViewById(R.id.txt_hurl);

RestAdapter radapter=new RestAdapter.Builder().setEndpoint(url).build();

MInterface restInt=radapter.create(MInterface.class);

restInt.getUser(new Callback<Pojo>() {
@Override
public void success(Pojo model, Response response) {
company.setText(“Company Name :”+model.getCompany());
blog.setText(“Website :”+model.getBlog());
htmlurl.setText(“Git URL :”+model.getHtmlUrl());
}
@Override
public void failure(RetrofitError error) {

String err = error.getMessage();
}
});

}
}

Screen Shot:

retrofit-android-mobilesiri-10

MobileSiri.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com. Read about our Affiliates Disclosure Policy here. Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.