Last night I saw Square guys have updated OkHttp wiki with Interceptors section for upcoming release of OkHttp 2.2.

Last point of Interceptors article says that OkHttp 2.2 is not available for current Retrofit version. Retrofit uses OkUrlFactory and executes the requests on HttpUrlConnection. It is possible to execute Retrofit requests directly on OkHttp, if we provide custom Client implementation.

Alright, have the implementation and this gist.

public class OkHttpClient22 implements Client {

private static OkHttpClient sClient = new OkHttpClient();

public OkHttpClient22(){};

public OkHttpClient22(OkHttpClient okClient){

sClient = okClient;

} @Override

public retrofit.client.Response execute(retrofit.client.Request request) throws IOException {

com.squareup.okhttp.Request okRequest = buildRequest(request);

Response okResponse = sClient.newCall(okRequest).execute();

retrofit.client.Response response = buildResponse(okResponse);

return response;

} public retrofit.client.Response execute(retrofit.client.Request request) throws IOException {com.squareup.okhttp.Request okRequest = buildRequest(request);Response okResponse = sClient.newCall(okRequest).execute();retrofit.client.Response response = buildResponse(okResponse);return response; private retrofit.client.Response buildResponse(Response okResponse) {

TypedInput inputBody = new ResponseBodyWrapper(okResponse.body());

retrofit.client.Response response =

new retrofit.client.Response(okResponse.request().urlString(), okResponse.code(), okResponse.message(), getHeaders(okResponse.headers()), inputBody);

return response;

} private List<Header> getHeaders(Headers headers) {

List<Header> retrofitHeaders = new ArrayList<Header>();

int headerCount = headers.names().size();

for(int i = 0; i < headerCount; i++){

retrofitHeaders.add(new Header(headers.name(i),headers.value(i)));

}

return retrofitHeaders;

} private com.squareup.okhttp.Request buildRequest(retrofit.client.Request request) {

com.squareup.okhttp.Request.Builder requestBuilder = new com.squareup.okhttp.Request.Builder();

// — -copy headers — -

List<retrofit.client.Header> headers = request.getHeaders();

Headers.Builder okHeadersBulder = new Headers.Builder();

if(headers != null && headers.size() > 0) {

for (Header h : headers) {

okHeadersBulder.add(h.getName(), h.getValue());

}

requestBuilder.headers(okHeadersBulder.build());

}

// — — copy url — —

requestBuilder.url(request.getUrl());

// — — copy method and create request body — -

if(!request.getMethod().equalsIgnoreCase(“GET”))

requestBuilder.method(request.getMethod(), new RequestBodyWrapper(request.getBody()));

else

requestBuilder.get();

return requestBuilder.build();

}

}

Retrofit uses TypedOutput implementation for request body, on the other hand OkHttp uses RequestBody. So we create a wrapper that will be mediator between these two. RequestBodyWrapper be wrapping TypedOutput to RequestBody and ResponseBodyWrapper be wrapping okhttp’s ResponseBody to Retrofit-friendly, TypedInput.

Usage

Pass this client when you are building your RestAdapter as following:

RestAdapter.Builder restAdapterBuilder = new RestAdapter.Builder();

restAdapterBuilder.setClient(new OkHttpClient22());

restAdapterBuilder.setExecutors(Executors.newCachedThreadPool(), new MainThreadExecutor());

restAdapterBuilder.setRequestInterceptor(new AuthorizationInterceptor());

restAdapterBuilder.setEndpoint(Constants.API_ENDPOINT);

restAdapterBuilder.setConverter(new GsonConverter(builder.create()));

restAdapterBuilder.setLogLevel(RestAdapter.LogLevel.FULL);

RestAdapter restAdapter = restAdapterBuilder.build();

Conclusion

Retrofit uses different implementation than OkHttp for constructing http requests and responses. There is no magic done here, both libraries are easy to use, hence wrapping the request and response bodies. Executing requests on OkHttp will provide you with efficiency and robustness. All we have to do is wait for 2.2 to come out.

Now, you can have best of both too.