How to make Network API call in Android.

A simple and easy to understand guide in creating a Network API call in android using Retrofit.

What is Retrofit?

In Android, Retrofit is a REST Client for Java and Android by Square inc under Apache 2.0 license. It is a simple network library used for network transactions. It makes it relatively easy to retrieve and upload JSON (or other structured data) via a REST-based web service.

What is API?

If I were to explain about API’s it will go beyond this article so I will give a simple analogy that everyone can understand.

Consider a vending machine, how does it work and what does it contain. A vending machine contains snacks and drinks similar to a server that contains the data we will need for processing.

In a vending machine, we put in coins and press certain buttons signifying the specific snack or drink. Similarly to get data from the server we need to specify what we need and this is called a request, what happens to the vending machine once we press these buttons it will give us the corresponding snack, this is called as a response while we are communicating with the server.

In order to perform this beautiful and very important task, we can make use of many libraries one of which is Retrofit.

How to?

select “Empty Activity” and click next

after entering an appropriate name for your application click on finish and wait for the build to complete.

2. In order to do this task, we will need a dummy REST API you can find many websites that provide this service where they will provide you with some sample API to play with or if you wish to make your own API please refer to this article to achieve just that.

for this example, I will be using one such dummy API” this is the API we will be using for this example.

3. Open up your activity_main.xml file and write the following code.

this will now create a button in your application on a click of which we will call the API.

4. Now before we bind this view with the program we need to have certain prerequisites for Retrofit to work, we need to add certain dependency in order for it to work.

5. Open your app-level Gradle file and paste the following code.

6. Now create an Interface and name it APIInterface. Here we will define all the methods (endpoints) that we will use for calling the API for different API we will use different methods and this Interface will help keep track of that.

7. Create another java class and name it APIClient and paste the following code. This class will create the instance of Retrofit and will be used to define the Base Url and verify the SSL certificate etc.

8. Now let us define the endpoints inside the Interface we created like this.

here “Response” is the POJO class we need to create we will go through that class next, create a class Response and place this code in it.

9. Now we are almost set up, but we didn't do something really important, open up the manifest file and provide internet permission, without this the application will crash.

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

10. Now we will bind the views and on click of the button, we will call the API.
Open up and write the following code.

here we are just printing a toast message of the response we are getting.

11. This is the response and we can play with it as much as we want.

“userId”: 1,
“id”: 1,
“title”: “delectus aut autem”,
“completed”: false

I know it’s not much.

Thank you for following up in the next article I will cover the topic recycler view and how to get data from API and put it into recycler view.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store