TheJudge
TheJudge

Reputation: 606

okhttp3 with Retrofit 2.0.0-beta4

I'm trying to get my network class to log incoming JSON, I'd like to use HttpLoggingInterceptor from OkHttp3. Problem is my Retrofit object wont take OkHttpClient from okhttp3. Please let me know if you know what I'm doing wrong.

Gradle

compile 'com.squareup.retrofit:retrofit:2.0.0-beta4'
compile 'com.squareup.okhttp3:okhttp:3.1.2'
compile 'com.squareup.retrofit:converter-jackson:2.0.0-beta2'
compile 'com.squareup.okhttp3:logging-interceptor:3.0.1'

Retrofit and client setup

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit.Retrofit;

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

retrofit = new Retrofit.Builder().baseUrl(URL).client(client).addConverterFactory(JacksonConverterFactory.create()).build();

API = retrofit.create(com.glasshouse.glasshouse.Network.API.class);

Android studio says Retrofit needs okhttp.OkHttpClient not okhttp3.OkHttpClient but if use that one, I can't use the HttpLoggingInterceptor ...

Thanks for any answer.

Upvotes: 0

Views: 4085

Answers (3)

TheJudge
TheJudge

Reputation: 606

Yeah nevermind - for those facing similar issues, use retrofit2 not retrofit

(Need to import retrofit2.Retrofit not import retrofit.Retrofit )

Upvotes: 1

You Qi
You Qi

Reputation: 9231

As a reference for others, you need to compile "com.squareup.retrofit2" and not "com.squareup.retrofit" in your gradle.

//old
compile 'com.squareup.retrofit:retrofit:2.0.0-beta4'

//new
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'

Upvotes: 1

Faraz Ahmed
Faraz Ahmed

Reputation: 1245

I am using following configuration for retrofit and it works well.

Inside build.gradle:

      compile 'com.squareup.okhttp3:okhttp:3.0.1'
      compile 'com.squareup.okhttp:logging-interceptor:2.6.0'
      compile 'com.fasterxml.jackson.core:jackson-core:2.4.1'
      compile 'com.fasterxml.jackson.core:jackson-annotations:2.4.1'
      compile 'com.fasterxml.jackson.core:jackson-databind:2.4.1'

Retrofit Configuration:

    private WebServices getWebServices(String serverUrl) {
    OkHttpClient client = new OkHttpClient();
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    client.interceptors().add(interceptor);
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(serverUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .client(client)
            .build();
    webServices = retrofit.create(WebServices.class);
    return webServices;
}

Upvotes: 0

Related Questions