9

我认为这些最新版本应该是兼容的。有这条推文; 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 接口。

我现在正在使用这种方法;https://medium.com/@nullthemall/execute-retrofit-requests-directly-on-okhttp-2-2-7e919d87b64e

我误解了变更日志吗?Maye Retrofit 1.9 可以在类路径中使用 OkHttpClient 2.2 但接口尚未适应?

4

1 回答 1

15

您正在传递OkHttpClientRestAdapter.Builderwhich 接受Client实现。,如果没有在实现中OkHttpClient使用,则与 Retrofit 无关。Client

您应该传递OkHttpClientOkClient实现的实例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();
于 2015-01-10T19:58:18.640 回答