Member-only story
How to Use Retrofit for Networking in Android
Making networking calls has never been easier
1. Adding the Dependencies
In your project’s build.gradle
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.google.code.gson:gson:2.8.7'
Also, be sure to add the INTERNET
permission in your manifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
2. Getting Started
For our tutorial, we will be using this particular end-point. Open the link and observe its JSON Response.

If you take a look at it, you will find that its an array of POST JSON Objects.
3. Create Model
It’s always a great practice to create the model of the data we are trying to consume in our application, so let’s do that.
4. Create your Networking Repository
- Create a package called ‘networking’ in your project structure.
- Create a new Java Interface inside it and give it a name
JsonPlaceHolderAPI
which is the name of the API we are trying to consume. - Depending upon the number of endpoints, create different methods. For us, we are just consuming the GET Posts endpoint. So we will create a method for it.
public interface JsonPlaceholderAPI {@GET("posts")
Call<List<Post>> getPosts();}
- Notice we mention the endpoint suffix inside the
@GET("posts")
that way Retrofit will know what is the endpoint. - We also mention the return type of data that we expect from this…