Android Retrofit 2 HTTP Client communicate with REST API

Android provide official way to make a http request by using Apache HttpClient or HttpUrlConnection bundle. The HttpClient is depreciated and removed in API 23 Android 6.0 and not success to use because it will be out dated soon. Besides, while new HttpUrlConnection have slow performance compare to http client library like retrofit, volley and okhttp. Therefore, i recommend the developer to use the latest retrofit 2 in your android application. It will provide you a better experience to the front user so your user will use your app. In this tutorial, I will teach you use the Android Retrofit 2 HTTP Client communicate with REST API in your application.

Creating a New Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “RetrofitExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently are support more than API 15. Click Next button.
4. Choose “Empty Activity” and Click Next button
5. Lastly, press finish button.

Add 3 Dependencies

Add retrofit and gson dependencies in your dependencies sections so you can will those later.


I use to create a sample rest api , this is the following url

Create a Class

Add a new class in your project name it to DoorResponse and this class in to get the response object from the rest api.

Create an Interface

Add a new interface and name it to GetDoorAPI. It will specific the rest api url, you can see in the following source code.

Edit activity_main.xml

Open your activity_main.xml to edit the layout, it will be display the response from the retrofit in the activity layout.

Edit class

Go to the main class and edit it to the bottom source code, it will perform the asynchrous retrofit call in this class and set text to the activity.

Add permission

Go to your AndroidManifest.xml and add the new permission which is to access the internet in your project.

Run your project

Now, you can run the project and you will see the content will be change immediately.

(Android Retrofit 2 HTTP Client communicate with REST API)

Source Code

(Visited 1,042 times, 1 visits today)

Yong Loon Ng

Ng Yong Loon, better known as Kristofer is a software engineer and computer scientist who doubles up as an entrepreneur.

You may also like...

1 Response

  1. October 22, 2016

    […] previous tutorial, I had tell you how to use retrofit 2 to retrieve data from the rest api. Able to let user to […]

Leave a Reply

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