Thursday, 29 December 2016

Example Tutorial Retrofit 2.0 Method GET Using Parameter

Hello everyone, today i going to share a nice tutorial about how to use method GET of Retrofit 2.0. I will get my profile by parameter.

Example :
My profile's ID is 1. So the url become
In this example i just use one data profile with ID = 1. You can improve your project by creating much profile.

As you can see at the video above, after success get data profile, the response will display all attribute of my profile like ID, NAME, JOB and AGE


First, in order we to able to use Retrofit 2.0, we have you compile the following libraries in your dependencies build.gradle (Module: app) :

// retrofit 2.0
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'

Add uses-permission in your AndroidManifest.xml like the following permission :

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


First, create an interface that define Method GET of Retrofit that we'll use to get data profile. Give it name as Paste the following code :

On code above, "myself" is the endpoint. Endpoint is a attribute after url ( @Query("id") used to define the parameter

Second, create new java class, give it name as Paste the following code :

This java class help us to set the output of process get data profile. You can see the output on the android studio logcat.

Third, create new java class, give it name as, in this class we define the URL, set the LoggingInterceptor and client. Paste the following code :

Forth, create new java class, give it name as This class will used to store the response of the JSON. Paste the following code :

The attribute of @SerializedName("...") must same as the attribute in the JSON. Example in the JSON write as "id", the serializedName become to @SerializedName("id")

Last, create layout and java class of MainActivity.

activity_main.xml, paste the following code :, paste the following code :

Done! Run your project, and hope it running well.