Retrofit Library in Android

Retrofit is REST API Client for Java.
It is developed by Square Inc. It uses OkHttp library for HTTP Request. It is a simple library that is used for network transaction.
It is a very easy and fast library to retrieve and upload the data via Rest based web service.

Adding Dependency in Build.gradle-

dependencies {
    implementation 'com.squareup.retrofit2:retrofit:2.1.0'
    implementation ''
    implementation 'com.squareup.retrofit2:converter-gson:2.1.0'

Adding Internet Permission in Manifest-

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

Retrofit mainly need three things which are following-

1. Retrofit Instance-

You can create Retrofit instance by Retrofit.Builder().
You have to specify base url and converter factory at the time of Retrofit instance creation as in the below example.

2. Interface –

public interface ApiCallInterface
   @POST("api_name") // use @POST if api is post.
   Call<CountryResponse> getResponseData();

3. Model Class-

Retrofit need a model class (POJO) for sending and receiving request.
Retrofit use the model class for parsing the server response by using convertors like Gson, Jackson.

Example –

In below example, we are going to display country name and country code in RecyclerView.
For which as in createRetroFitBuilder() method of example first we set header using OkHttpClient class because API is authenticate api (set header only if api is authenticate). Then we create Retrofit instance and set base url and converter factory and call interface method which interact with API on background thread and success  of API can be get in onResponse() method of enqueue call back.Failure of API can be get in onFailure() method of enqueue call back.After that pass the list of country at adapter as in method setUpAdapterView() of example.

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerViewCountry;
    private CountryListAdapter countryListAdapter;
    String BASE_URL = "http://example/retrofit/api/";
    CountryResponse countryResponse;
    protected void onCreate(Bundle savedInstanceState) {

    private void initializeView()
        recyclerViewCountry = (RecyclerView) findViewById(;

    private void createRetroFitBuilder() {
        // set header through OkHttpClient if API is authenticate API.
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

        httpClient.addInterceptor(new Interceptor() {
            public okhttp3.Response intercept(Chain chain) throws IOException {
                Request request = chain.request().newBuilder()
                        .addHeader("timestamp", "153138130")
                        .addHeader("authentication_key", "QJTpP/7rai7D7KF2RcNK=")
                return chain.proceed(request);

        // creating retrofit object and set base url , Converter factory
        Retrofit retrofit = new Retrofit.Builder()
        // calling of interface method which interact with API and give response in onResponse().
        ApiCallInterface apiCallInterface = retrofit.create(ApiCallInterface.class);
        Call<CountryResponse> call = apiCallInterface.getResponseData();

        call.enqueue(new Callback<CountryResponse>() {
            public void onResponse(Call<CountryResponse> call, Response<CountryResponse> response)
                countryResponse = response.body();

            public void onFailure(Call<CountryResponse> call, Throwable t) {

    private void setUpAdapterView() {
        if(countryResponse != null)
            countryListAdapter = new CountryListAdapter(countryResponse,this);
            LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);




Leave a Reply