我使用Retrofit 1.9.0和OkHttp 2.0.0
compile 'com.squareup.okhttp:okhttp:2.0.0' compile 'com.squareup.retrofit:retrofit:1.9.0'
但是没有找到拦截器方法
OkHttpClient client = new OkHttpClient(); client.setConnectTimeout(50000, TimeUnit.MILLISECONDS); client.setReadTimeout(50000, TimeUnit.MILLISECONDS); client.interceptor().add(...); // cannot resolve method interceptor
我正在处理改造中的重试请求,这篇文章建议 如何使用OkHttp/Retrofit重试HTTP请求?
我在这里错过了一些图书馆吗?
试试这样:
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'
在您的客户端类中:
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);