A Beginner’s Guide to Retrofit in Android (Java)

<p>In the world of Android app development, making network requests to fetch data from APIs is a common task. Retrofit is a powerful and popular library that simplifies this process by providing a clean and efficient way to make API calls. In this tutorial, we&rsquo;ll walk through how to use Retrofit to fetch a list of todos from a mock API and display their titles in a ListView using an ArrayAdapter. For this tutorial, we&rsquo;ll use the mock API &ldquo;https://jsonplaceholder.typicode.com/todos&quot;.</p> <p>Step 1: Add internet permission</p> <p>Before your Android app can communicate with remote servers or APIs, you need to grant it the necessary permissions. To enable internet access for your app, you&rsquo;ll need to add the &ldquo;INTERNET&rdquo; permission to your app&rsquo;s AndroidManifest.xml file. Add the following line inside the &lt;manifest&gt; tag:</p> <pre> &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;</pre> <p>Step 2: Add the Retrofit library</p> <p>To add the Retrofit library to your project, you&rsquo;ll need to open the&nbsp;<code>build.gradle</code>&nbsp;file for your app module. In Android Studio, you can find it by expanding the&nbsp;<code>Gradle Scripts</code>&nbsp;folder.</p> <p>Once you have the&nbsp;<code>build.gradle</code>&nbsp;file open, you&rsquo;ll need to add the following dependencies to the&nbsp;<code>dependencies</code>&nbsp;block:</p> <pre> dependencies { implementation &#39;com.squareup.retrofit2:retrofit:2.9.0&#39; implementation &#39;com.squareup.retrofit2:converter-gson:2.9.0&#39; }</pre> <p>The first dependency,&nbsp;<code>implementation &#39;com.squareup.retrofit2:retrofit:2.9.0&#39;</code>, adds the Retrofit library to your project. This library provides the core functionality for making HTTP requests and handling responses.</p> <p>The second dependency,&nbsp;<code>implementation &#39;com.squareup.retrofit2:converter-gson:2.9.0&#39;</code>, adds a converter for Gson to your project. This converter allows Retrofit to automatically convert JSON data into Java objects using the Gson library.</p> <p>After adding these dependencies, make sure to sync your project with Gradle by clicking on the&nbsp;<code>Sync Now</code>&nbsp;button that appears in the top right corner of Android Studio.</p> <p>Step 3: Add a POJO class</p> <p>After adding the Retrofit and Gson dependencies to your project, the next step is to create a&nbsp;<code>models</code>&nbsp;directory and add a POJO (Plain Old Java Object) class named&nbsp;<code>Todo</code>. This class will represent the data model for a todo item that we&rsquo;ll be fetching from the API.</p> <p>Here&rsquo;s how you can create the&nbsp;<code>models</code>&nbsp;directory and add the&nbsp;<code>Todo</code>&nbsp;class:</p> <ol> <li>In Android Studio, right-click on the&nbsp;<strong>com.exmple.yourappname&nbsp;</strong>(In my case it is com.exmple.retrofitexample) directory in your app module and select&nbsp;<code>New</code>&nbsp;&gt;&nbsp;<code>Package</code>.</li> <li>Enter&nbsp;<code>models</code>&nbsp;as the package name and click&nbsp;<code>OK</code>.</li> <li>Right-click on the newly created&nbsp;<code>models</code>&nbsp;package and select&nbsp;<code>New</code>&nbsp;&gt;&nbsp;<code>Java Class</code>.</li> <li>Enter&nbsp;<code>Todo</code>&nbsp;as the class name and click&nbsp;<code>OK</code>.</li> </ol> <p>Now you should have a new&nbsp;<code>Todo.java</code>&nbsp;file in the&nbsp;<code>models</code>&nbsp;package. This file will contain the definition of the&nbsp;<code>Todo</code>&nbsp;class.</p> <p>The&nbsp;<code>Todo</code>&nbsp;class is needed for Retrofit because it defines the data model for the todo items that we&rsquo;ll be fetching from the API. Retrofit uses this class to automatically convert the JSON data returned by the API into Java objects that we can use in our app.</p> <p><a href="https://medium.com/@nikhil725051/a-beginners-guide-to-retrofit-in-android-java-d95bcc257fdc">Visit Now</a></p>