Retrofit API for Android

Aug 11, 2017





Retrofit  is a type-­safe REST client for Android built by Square. Retrofit makes it easy to connect to a REST web service by translating the API into Java interfaces.Using this tool we can make all network stuff much more easier. In this tutorial, I’ll show you how to use one of the most popular and often recommended HTTP libraries available for Android using Post request method.

Step 1: Creating a New Project in Android Studio

Create a new project in Android Studio from File ⇒ New Project. When it prompts you to select the default activity, select Empty Activity and proceed.
2.Open build.gradle and following dependencies.

3. Add Internet Permission in AndroidManifest.xml  file.

4 .Create three  sub packages named activity,model and rest in main package.Now,Move MainActivity under activity package.

Step2: Generating Models

We would like to make a POSTrequest (create a new resource) on the API. But before we execute this request, we need to know the JSON response we should expect when it is executed successfully so that Retrofit can parse the JSON response and deserialize it to Java objects.
According to APi,if we will send following data in POST request:

we will get following Response:

1.Create a under model package .

2.create a class under model package .

3.Creating  a Retrofit Instance

To send network requests to an API, we need to use the Retrofit Builder class and specify the base URL for the service.So create a under rest package.

4.Define Endpoints

The endpoints are defined inside of an interface using special retrofit annotations to encode details about the parameters and request method. In addition, the return value is always a parameterized Call<T> object such as Call<ApiResponse>.

Create under rest package.

Looking at the ApiResponse class, we have a method called savePost(). On top of the method is the @POST annotation, which indicates that we want to execute a POST request when this method is called. The argument value for the @POST annotation is the endpoint—which is /posts. So the full URL will be

Here, @FormUrlEncoded will indicates  that the request will have its MIME type (a header field that identifies the format of the body of an HTTP request or response) set to application/x-www-form-urlencoded and also that its field names and values will be UTF-8 encoded before being URI-encoded. The @Field(“key”) annotation with parameter name should match the name that the API expects. Retrofit implicitly converts the values to strings using String.valueOf(Object), and the strings are then form URL encoded. null values are ignored. 

5.Creating Api utility class

Create a class named ApiUtils under rest package.


This class has a base URL as a static variable,And it provides APiInterface with a getAPIService method.

6.Creating the  Layout for MainActivity.

In activity_main.xml file we will have one EditText for Email ,and Submit button for posting data to the API.below the button,we will take LinearLayout with multiple textviews and ListView,for displaying our Post API response data.

 All the strings used in above layout are from string.xml under res->values folder.

7.Executing POST Request.

In the onCreate method of MainActivity  we will  intitalize instance of  the ApiInteface and other necessary views of activity_main.xml.

 We will call sendPost() method on click of submit button.In the sendPost(String) method  used above,we will pass the  Entered value of EditText etEmail . this method will call our API service interface method savePost(String)whose job is to execute a POST request sending the email to the API. The showResponse(String response) method will display the response on the screen.
Our APIService instance mAPIService method savePost(String) will return a Call instance which has a method called enqueue(Callback<T> callback).
enqueue() asynchronously sends the request and notifies your app with a callback when a response comes back. To use the enqueue() method, you have to implement two callback methods: 

  • onResponse(): invoked for a received HTTP response. To get the status code in order for you to handle situations based on them, you can use the method response.code(). You can also use the isSuccessful() method to find out if the status code is in the range 200-300, indicating success.
  • onFailure(): invoked when a network exception occurred communicating to the server or when an unexpected exception occurred handling the request or processing the response.

Step 10.Run your App

FinallyYou can Run the App,after entering email in EditText field.Then click on submit button and It displays Response from the API in respected fields.So,Your final output will be:







Post by Vishwa Hirani

Leave a Reply

Your email address will not be published. Required fields are marked *