Android Retrofit Version 2.0 with OKHttp and Gson Tutorial for Beginners

Introduction

In the previous tutorial, you have learned how to consume Web Services in Android in which we used HttpURLConnection to send and receive data over the internet and JSONObject & JSONArray classes to parse JSON data in Android along with AsyncTask class to perform background actions. In this tutorial, you will learn Retrofit Version 2.0 library with OKHttp, Gson and GsonConverter libraries to parse and receive JSON data from the internet.

Retrofit Version 2.0

Retrofit turns your HTTP API into a Java interface. Previous libraries are slow and now been replaced with Retrofit. With Retrofit you can easily perform the same tasks with more efficiency and much faster than the old methodologies. So to use Retrofit you will need to implement a Rest Interface and parse the data using Gson and GsonConverter. If you want to see the complete Retrofit Package on GitHub, click here.

Consider the below example to learn how to use Retrofit.

Get Country Information by using Retrofit Version 2.0

To implement this example, you will need to configure for Retrofit Version 2.0 with OKHttp, Gson and GsonConverter libraries.

Adding Dependencies

Open gradle.build (Module: app) and add required dependencies.

References:
Click here for OKHttp.

After adding the dependencies sync the project.

Adding Internet Permissions

Open Android Manifest file and add the internet permissions in <manifest> tags.

Generating POJO Classes

Generate POJO from JSONSchema2POJO by placing the JSON format code in the editor and setting the Source Type to JSON and Annotation Style to Gson. Download the zip files from the ZIP button at the bottom of the page.

For the REST countries POJO, get the JSON data from here. Copy and paste the JSON into the Editor and get POJO files.

JSON to GSON from jsonschema2pojo
JSON to GSON from jsonschema2pojo

You can download POJO classes for Android REST Countries API from here as well.

Move POJO classes to the project source folder.

 

Java Interface for Retrofit Version 2.0

Create an interface; I named the interface as RestApi. In the interface add a retrofit Version 2.0 HTTP get annotation with the API URL parameters.

Add retrofit Call as POJO class parameters with a method to be called when accessing the POJO data. This is the method through which you can communicate with Retrofit to get the data. CountriesInfo is the POJO class generated from the JSON data of REST countries API.

I am passing a string to dynamically change the URL in Retrofit REST API interface.

 

Create Layout of the Android Application

I am modifying the previous example of Android web services. So I will keep my layout same.

Android Activity for Web Services
Android Activity for Web Services

You can find the complete code of the layout at the end or this tutorial.

Now move toward ActivityMain.java file. Implement the button click handler and get the text from the text field. Also, check whether the text field has valid data or not.

Parse JSON data using GsonConverter

 

Executing Call from Interface

Implement the enqueue method of the call.

The enqueue require Callback<POJO-CLASS> as parameters. To Implement enqueue method, you must override onResponse() to get the response from the service and onFailure() to get the error details.

Initialize TextViews to Display Data from Web Service

Set Data in TextViews

Use response.body() to get the response data from the service. These get methods are from POJO class.

You are now done with the countries information application

Download the Complete Retrofit Example

Pictorial Understanding

Pictorial Understanding of Retrofit Version 2.0 Countries Info Application Code
Pictorial Understanding of Retrofit version 2.0 Countries Info Application Code

Complete Code for ActivityMain.java

Complete Code for the Layout

Download Retrofit Example Using REST Countries API

Hope that you like this tutorial. Stay tuned for more upcoming tutorials. Stay Blessed!

7 thoughts on “Android Retrofit Version 2.0 with OKHttp and Gson Tutorial for Beginners

      1. i have checked all dependencies and update my build.gradle but not able to fetch api data, NO error but the response.body() is always null in this code.

          1. i am doing same as you done infect i downloaded code and installed apk on my phone still not working, Can you tell me on what API level you have tested this?

Leave a Reply

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