Retrofit has become a very familiar name in the Android ecosystem for a very useful library. One that turns your HTTP API into a Java (or Kotlin) interface. In layman’s terms, it makes loading APIs into POJOs ridiculously simple and we’re going to be following that trend as we learn how to implement that.

How it works is you have a model class which is a POJO. Retrofit loads in the API directly into your model class so the result of your API call is an instance or a list of your model class.

This tutorial’s going to just scrape the surface of Retrofit, fitting if you don’t know a single thing about how to implement it yet. No wasting time or brain power with authentication and all that jargain just yet.

For this tutorial, I will be using the JSON Placeholder API, though if you want to reap the most benefit out of this tutorial, I suggest you try it out using any other popular free APIs like OpenWeatherMap or OMDb.

This is the URL for the API I’ll be using and it’ll return a result like this:

Import the Dependencies

Add these dependencies to your app/build.gradle file.

Defining the Model

We want to set up our model as a POJO (Plain Old Java Object). What this essentially means is a class with nothing but their variables and their getter/setter methods. In Kotlin, getter/setters are automatically defined so we don’t need to write them ourselves.

You want to match the variable names to the API fields. If you want to put a more fitting name for the variable in your app than the name in the API, annotate it with SerializedName(“apifield”) to match the variable to the API field.

Write the Interface

Create a new interface where you will place all your Retrofit calls.

Each call is a function annotated with @GET(“urlextension”) or @POST(“urlextension”), depending on the action you’re performing.

Build the Retrofit

A Retrofit is the object that makes all your Retrofit calls. In your main code, create a retrofit object using the builder, defining the Base URL of your APIs and the Converter Factory as the GsonConverterFactory.

Then get an instance of your API Interface by using retrofit.create.

Make the Call

Just like that, we can use the API instance to make our Retrofit calls!

Use a callback to get the results of your API call and you’ve basically got your data!

Spice it up with RxJava

I am a huge fan of RxJava for its huge multitude of benefits for pretty much any app, and it works excellently with Retrofit. Strengthen their interactions by adding this dependency as well as the RxJava and RxAndroid dependencies if you haven’t already got them in.

Where you build your Retrofit object, define the CallAdapterFactory like so, otherwise your app will crash.

Change your interface to return an Observable instead of a Call.

Then finally, make the call as an observable