• 8
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Retrofit 1.9 with OkHttp 2.2 and interceptors

I thought that these recent versions were supposed to be compatible. There is this tweet; https://twitter.com/JakeWharton/status/553066921675857922 and the changelog of Retrofit 1.9 mentions it too.

However when I try this:

        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();

It still doesn't work. The setClient method complains about an incompatible Client object;

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)

What am I missing? I also see OkHttpClient does not implement the Client interface.

I am using this approach for now; https://medium.com/@nullthemall/execute-retrofit-requests-directly-on-okhttp-2-2-7e919d87b64e

Did I misinterpret the changelog? Maye Retrofit 1.9 can uses OkHttpClient 2.2 when it's in the classpath but the interface isn't adapted yet?

You are passing OkHttpClient to the RestAdapter.Builder which accepts Client implementations. OkHttpClient, solely, is not related to Retrofit if not used in Client implementations.

You should pass OkHttpClient to OkClient instance that implements 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();
  • 15
Reply Report