Okhttp - OkHttpClient 2.0.0无法解析方法拦截器()

Zab*_*iel 1 android retrofit okhttp

我使用Retrofit 1.9.0和OkHttp 2.0.0

compile 'com.squareup.okhttp:okhttp:2.0.0'
compile 'com.squareup.retrofit:retrofit:1.9.0'
Run Code Online (Sandbox Code Playgroud)

但是没有找到拦截器方法

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

我正在处理改造中的重试请求,这篇文章建议 如何使用OkHttp/Retrofit重试HTTP请求?

我在这里错过了一些图书馆吗?

Dra*_*ake 6

试试这样:

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'
Run Code Online (Sandbox Code Playgroud)

在您的客户端类中:

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);
Run Code Online (Sandbox Code Playgroud)