android – 使用OkHttp 2.2和拦截器的Retrofit 1.9

前端之家收集整理的这篇文章主要介绍了android – 使用OkHttp 2.2和拦截器的Retrofit 1.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不实现客户端界面.

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

我是否误解了变更日志? Maye Retrofit 1.9可以在类路径中使用OkHttpClient 2.2,但是界面还没有改编?

解决方法

您正在将OkHttpClient传递给接受Client实现的RestAdapter.Builder. OkHttpClient,仅在客户端实现中不使用,与Retrofit无关.

您应该将OkHttpClient传递给实现Client的OkClient实例

.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();
原文链接:https://www.f2er.com/android/311321.html

猜你在找的Android相关文章