Over a million developers have joined DZone.

5 Ways to Customize Retrofit Library in Android

DZone's Guide to

5 Ways to Customize Retrofit Library in Android

This how-to provides tips, with code, on customizing Android Retrofit Library, including enabling logging and overriding connection timeouts.

· Mobile Zone ·
Free Resource

Retrofit is a type-safe HTTP client for Android built on top of OkHttp Stack. Most of the boilerplate code is hidden inside the library, making your code clean, maintainable, and easily extensible. In this article, I will discuss five ways to customize Retrofit Library. For a simple step-by-step guide on implementing the Retrofit library, see this Retrofit Android tutorial.

1. Adding a Custom Header in Retrofit

Sometimes you need to add a custom header where the goal is to override a certain property of a request or to pass some data about a client. The example below shows how to add an authorization to every request.

Setting Up

  • Add the following dependency in your build.gradle:  
compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'
  • Create a new class called CustomInterceptor.java, that implements the Interceptor interface provided by Okhttp3. To add a custom header, you should add your header property in the intercept() method. See the example below:
    public class CustomInterceptor implements Interceptor {
        public Response intercept(Chain chain) throws IOException {
            Request.Builder builder = chain.request().newBuilder();
            builder.addHeader("authorization", "c28b544426db927b5d1caa8b00ae50f8");
            return chain.proceed(builder.build());

2. Passing Different JSON Structures to a Single API

To perform a post request, you need to pass your request object to API. This prevents sending a different type of object to your API. For example, consider passing a filter query to your API. Filter parameters may include price, relevance, color, etc. For a normal case, you may need to create a four API that filters according to the different property. See below:

    Call<OtpVerificationResponse> filter(@Body Price price);

    Call<OtpVerificationResponse> filter(@Body Relevance relevance);

To overcome this limitation, you can create only one API as shown below:

    Call<OtpVerificationResponse> filter(@Body Object object);

The above API will accept any type of an object.

3. Overriding a Connection Timeout

To override a connection timeout, you just need to create an object of OkHttpClient and pass it in Retrofit builder method, as shown below:

Creating an OkHttpClient Object With a Custom Timeout

  • final OkHttpClient okHttpClient = new OkHttpClient.Builder()
        .readTimeout(60, TimeUnit.SECONDS)
        .connectTimeout(60, TimeUnit.SECONDS)

  • Passing an OkHttpClient Object to Retrofit Builder Method

    retrofit = new Retrofit.Builder()

    4. Working With Different Response Types

    Web service may vary in terms of response format. Some web services return a response in JSON, while others are in XML. You don't need to change your code in order to work with different response formats. You can simply override the converter type in the Builder method of Retrofit. See the example below:

    For JSON Converter Using GSON

    • Add the following dependency in your build.gradle file:  compile 'com.squareup.retrofit2:converter-gson:2.1.0' 
    • Change the converter type while building a Retrofit object:
      Retrofit retrofit = new Retrofit.Builder()

    For XML converter, see Sourceforge documentation. 

    5. Enabling Logging

    To enable logging, add the following dependency in your build.gradle file:

     compile 'com.squareup.okhttp3:logging-interceptor:3.3.1' 

    Make the following changes to your code:

     OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        Retrofit retrofit = new Retrofit.Builder()

    For more tutorials like this, see Learnzone.

    Happy coding!

    android ,retrofit ,mobile ,retrofit library ,mobile development

    Opinions expressed by DZone contributors are their own.

    {{ parent.title || parent.header.title}}

    {{ parent.tldr }}

    {{ parent.urlSource.name }}