我认为这些最新版本应该兼容。有这条推文;
https://twitter.com/JakeWharton/status/553066921675857922和Retrofit 1.9的变更日志也提到了它。
但是,当我尝试这样做:
OkHttpClient httpClient = new OkHttpClient();
httpClient.interceptors().add(new TokenExpiredInterceptor());
mRestAdapter = new RestAdapter.Builder()
.setEndpoint(API_ENDPOINT)
.setClient(httpClient)
.setLogLevel(BuildConfig.DEBUG ?
RestAdapter.LogLevel.FULL :
RestAdapter.LogLevel.NONE)
.setRequestInterceptor(new AuthorizationInterceptor())
.build();
它仍然不起作用。 setClient方法提示不兼容的Client对象。
Error:(29, 21) error: no suitable method found for setClient(OkHttpClient)
method Builder.setClient(Client) is not applicable
(argument mismatch; OkHttpClient cannot be converted to Client)
method Builder.setClient(Provider) is not applicable
(argument mismatch; OkHttpClient cannot be converted to Provider)
我想念什么?我也看到OkHttpClient没有实现Client接口(interface)。
我现在正在使用这种方法。 https://medium.com/@nullthemall/execute-retrofit-requests-directly-on-okhttp-2-2-7e919d87b64e
我是否误解了变更日志? Maye Retrofit 1.9在类路径中但接口(interface)尚未适应时可以使用OkHttpClient 2.2吗?
最佳答案
您正在将OkHttpClient
传递到接受RestAdapter.Builder
实现的Client
。如果未在OkHttpClient
实现中使用,则Client
仅与翻新无关。
您应该将OkHttpClient
传递给实现OkClient
的Client
实例.setClient(new OkClient(httpClient))
OkHttpClient httpClient = new OkHttpClient();
httpClient.interceptors().add(new TokenExpiredInterceptor());
mRestAdapter = new RestAdapter.Builder()
.setEndpoint(API_ENDPOINT)
.setClient(new OkClient(httpClient))
.setLogLevel(BuildConfig.DEBUG ?
RestAdapter.LogLevel.FULL :
RestAdapter.LogLevel.NONE)
.setRequestInterceptor(new AuthorizationInterceptor())
.build();