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.
Join the DZone community and get the full member experience.
Join For FreeRetrofit 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 { @Override 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:
@POST("/filter")
Call<OtpVerificationResponse> filter(@Body Price price);
@POST("/filter")
Call<OtpVerificationResponse> filter(@Body Relevance relevance);
To overcome this limitation, you can create only one API as shown below:
@POST("/filter")
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)
.build();
Passing an OkHttpClient Object to Retrofit Builder Method
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
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()
.baseUrl(EndPoint.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
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();
httpClient.addInterceptor(logging);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(EndPoint.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
For more tutorials like this, see Learnzone.
Happy coding!
Opinions expressed by DZone contributors are their own.
Comments