Logging with Retrofit 2
Retrofit has been updated to 2.0 version.
It's a major change in the one of the most popular library for Android platform. A lot of things have been changed out there but in this blog post I want to cover how to setup logging properly.
In Retrofit 1.x we just call:
In Retrofit 2 you should use HttpLoggingInterceptor.
Add dependency to
Retrofit object like follow:
OkHttpClient client = new OkHttpClient(); HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); client.interceptors().add(interceptor); Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://backend.example.com") .client(client) .addConverterFactory(GsonConverterFactory.create()) .build(); return retrofit.create(ApiClient.class);
RetrofitAdapter doesn't exsist any longer, we have
Retrofit class instead.
The big change is that OkHttp is required now and set as dependency for Retrofit.
The instance of
OkHttpClient is the place where you should set up logging.
Retrofit has a dependency to OkHttp version 2.5.0, but I've tested it with LoggingIntenceptor version 2.6.0 and it works well.
It should print logs similar to the old ones from Retrofit 1.x.
Hope it helps you :)
See this post on my personal blog.