2

Im using Retrofit 1.9.0 together with OkHttp 2.0.0

compile 'com.squareup.okhttp:okhttp:2.0.0'
compile 'com.squareup.retrofit:retrofit:1.9.0'

but interceptor method is not found on it

OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(50000, TimeUnit.MILLISECONDS);
client.setReadTimeout(50000, TimeUnit.MILLISECONDS);
client.interceptor().add(...); // cannot resolve method interceptor

I'm currently dealing with retry-request on retrofit, suggested from this post How to retry HTTP requests with OkHttp/Retrofit?

am i missing some libraries here?

Community
  • 1
  • 1
Zabdiel
  • 21
  • 1
  • 2

2 Answers2

7

try like this:

compile 'com.google.code.gson:gson:2.5'
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
compile 'com.squareup.okhttp3:okhttp:3.0.1'

in your Client class:

OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .addInterceptor(
                    new Interceptor() {
                        @Override
                        public Response intercept(Interceptor.Chain chain) throws IOException {
                            Request request = requestBuilder.build();
                            return chain.proceed(request);
                        }
                    })
            .build();

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .client(okHttpClient)
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    restAPI = retrofit.create(RestAPI.class);
Pasha Shkaran
  • 1,433
  • 2
  • 23
  • 41
1

Also IMHO use retrofit 2.0 (although its still in beta, in my opinion its mature enough). You can

compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'
compile 'com.squareup.retrofit2:retrofit-converters:2.0.0-beta3'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'

Also for both 1.9 and 2, There are excellent series of blogs which I found useful in my learning Retrofit better. You can find them here

Edit: Since Retrofit 2 relies on OkHttp for network operations, you will not need to add OkHttp dependency explicitly once you have added the retrofit2

Neil B
  • 116
  • 1
  • 5