Retrofit is a type-safe REST client for Android built by Square. Using this library, android developer can make all network stuff much more easier. Retrofit will make the Http requests simpler and easy to handle the service calls. Retrofit will automatically convert JSON to Java objects and vice versa so that developer need not worry about marshaling and un-marshaling of the http request and response on their own.
API calls can be consumed in ASYNC manner or SYNC manner.
In this example I have shown Retrofit Async call which is widely used .
For testing you can go through this link and practice.
https://jsonplaceholder.typicode.com/
I have used https://jsonplaceholder.typicode.com/comments API which will return Array of Comments JSON data.
We can divide this API as Base Url + API Endpoint.
Base Url = https://jsonplaceholder.typicode.com/
API Endpoint = comments
Follow the below steps to make Retrofit Async call.
API calls can be consumed in ASYNC manner or SYNC manner.
In this example I have shown Retrofit Async call which is widely used .
For testing you can go through this link and practice.
https://jsonplaceholder.typicode.com/
I have used https://jsonplaceholder.typicode.com/comments API which will return Array of Comments JSON data.
We can divide this API as Base Url + API Endpoint.
Base Url = https://jsonplaceholder.typicode.com/
API Endpoint = comments
Follow the below steps to make Retrofit Async call.
Step 1: Add the below library to gradle file and sync it.
Step 2: Create a wrapper class for Comment.
By seeing the JSON data of thi api https://jsonplaceholder.typicode.com/comments , design the wrapper class and generate the getters and setters for the class. Name of the members in class must be same as that in keys of JSON data if not you have to use the annotations.
By seeing the JSON data of thi api https://jsonplaceholder.typicode.com/comments , design the wrapper class and generate the getters and setters for the class. Name of the members in class must be same as that in keys of JSON data if not you have to use the annotations.
API Response
Comments.java
Step 3 : Create an Interface for GET method with respect to API Endpoint.
Step 4: Define The retrofit client
Only one instance of retrofit client is fine for one application and u can reuse this as many times for making the service call.we make the service call.
As you can see in the above code, the method is static, since throughout the application we will be using only one instance of retrofit. There is no need to create instance of retrofit for every service call. First time it will be check for null and initialize the object and in the next scenarios just retrofit instance is returned.
As you can see in the above code, the method is static, since throughout the application we will be using only one instance of retrofit. There is no need to create instance of retrofit for every service call. First time it will be check for null and initialize the object and in the next scenarios just retrofit instance is returned.
Step 5: Making the Service.
The service call is made using the following code and response can be used for displaying or you can handle it as you want.
The service call is made using the following code and response can be used for displaying or you can handle it as you want.