Retrofit — How to use OkHttp 3 with Retrofit 1

The latest release of Retrofit 1 (1.9.0) directly depends on OkHttp 2.2.0. For Retrofit 2, the developers introduced multiple breaking changes, because Retrofit in its second major release actually uses OkHttp instead of working with and around it. That means, OkHttp got a lot attention and improvements in regard to functionality and stability. Also, the third major version was just released!

This guide will show you how to make use of OkHttp 3 even though you’re bound to Retrofit 1.


Retrofit Series Overview

  1. Getting Started and Creating an Android Client
  2. Retrofit 2 — Basics of API Description
  3. Retrofit 2 — Creating a Sustainable Android Client
  4. Synchronous and Asynchronous Requests
  5. Send Objects in Request Body
  6. Add Custom Request Header
  7. Retrofit 2 — Manage Request Headers in OkHttp Interceptor
  8. Retrofit 2 — Dynamic Request Headers with @HeaderMap
  9. Multiple Query Parameters of Same Name
  10. Optional Query Parameters
  11. Using the Log Level to Debug Requests
  12. Retrofit 2 — Log Requests and Responses
  13. Retrofit 2 — Enable Logging for Development Builds Only
  14. How to Upload Files to Server
  15. Retrofit 2 — How to Upload Files to Server
  16. Retrofit 2 — How to Upload Multiple Files to Server
  17. Retrofit 2 — Passing Multiple Parts Along a File with @PartMap
  18. Basic Authentication on Android
  19. Token Authentication on Android
  20. OAuth on Android
  21. Retrofit 2 — Hawk Authentication on Android
  22. How to Refresh an Access Token
  23. Retrofit 2 — Upgrade Guide from 1.9
  24. Retrofit 2 — Simple Error Handling
  25. How to use OkHttp 3 with Retrofit 1
  26. Send Data Form-Urlencoded
  27. Send Data Form-Urlencoded Using FieldMap
  28. Retrofit 2 — How to Add Query Parameters to Every Request
  29. Retrofit 2 — Add Multiple Query Parameter With QueryMap
  30. Retrofit 2 — How to Use Dynamic Urls for Requests
  31. Retrofit 2 — URL Handling, Resolution and Parsing
  32. Retrofit 2 — Constant, Default and Logic Values for POST and PUT Requests
  33. Retrofit 2 — How to Download Files from Server
  34. Retrofit 2 — Cancel Requests
  35. Retrofit 2 — Reuse and Analyze Requests
  36. Retrofit 2 — How to Change API Base Url at Runtime
  37. Optional Path Parameters
  38. Retrofit 2 — How to Send Plain Text Request Body
  39. Retrofit 2 — Pagination Using Query Parameter
  40. Retrofit 2 — Pagination Using Link Header and Dynamic Urls (Like GitHub)
  41. Retrofit 2 — Pagination Using Range Header Fields (Like Heroku)
  42. How to Integrate XML Converter
  43. Define a Custom Response Converter
  44. Retrofit 2 — Introduction to (Multiple) Converters
  45. Retrofit 2 — Adding & Customizing the Gson Converter
  46. Retrofit 2 — Implementing Custom Converters
  47. Retrofit 2 — Basics of Mocking Server Responses
  48. Retrofit 2 — Customizing Network Behavior of Mocked Server Responses (soon)
  49. Java Basics for Retrofit — Callbacks (soon)
  50. Java Basics for Retrofit — Annotations (soon)
  51. Java Basics for Retrofit — Fluent Interface with Builders (soon)

Dependencies

At first you need to add OkHttp 3 to your project. At the time of writing this post, OkHttp 3 was just released, that
means we can directly use the fresh baked cookie :)

Furthermore, you’ll need the wrapper class from Jake Wharton’s retrofit1-okhttp3-client repository on GitHub. Jake already released the client code and you can directly make use of this little helper by importing the Gradle or Maven library. The following code snippets show you the required imports to make Retrofit 1 work with OkHttp 3.

Gradle

compile 'com.squareup.retrofit:retrofit:1.9.0'  
compile 'com.squareup.okhttp3:okhttp:3.0.0'  
compile 'com.jakewharton.retrofit:retrofit1-okhttp3-client:1.0.2'  

Maven

<dependency>  
  <groupId>com.squareup.retrofit</groupId>
  <artifactId>retrofit</artifactId>
  <version>1.9.0</version>
</dependency>  
<dependency>  
  <groupId>com.squareup.okhttp3</groupId>
  <artifactId>okhttp</artifactId>
  <version>3.0.0</version>
</dependency>  
<dependency>  
  <groupId>com.jakewharton.retrofit</groupId>
  <artifactId>retrofit1-okhttp3-client</artifactId>
  <version>1.0.2</version>
</dependency>  

Use OkHttp 3 Client in RestAdapter

After adding the dependencies to your build.gradle file and finishing the synchronization, you’re ready to plug in the OkHttp 3 client.

The setClient() method on the RestAdapter.Builder expects a Client implementation. Within OkHttp 2, the implementation was called OkClient. For OkHttp 3, Jake Wharton named it Ok3Client and that’s exactly the only thing to change. We just need to pass an Ok3Client object as the parameter for the setClient method.

RestAdapter.Builder builder = new RestAdapter.Builder()  
        .setClient(new Ok3Client(new OkHttpClient()))
        .setEndpoint(API_BASE_URL);

Keep an eye on the OkHttpClient and make sure you’re using the OkHttp 3 version. Because OkHttp introduces the new group id called com.squareup.okhttp3 you’re able to have OkHttp 2 and 3 within your project. The Ok3Client implements the required Client interface, which means there’s nothing more to do than enjoy the new features and improvements of OkHttp 3.

OkHttp 3 Advantages and Features

With the update to OkHttp 3, you’re able to pick up and use the functionality like powerful interceptors. The thing you need to do is using your OkHttpClient for request and response interception instead of Retrofit’s interceptor.

The following code block outlines how to use an OkHttp 3 client with added interceptors and an Authenticator. The advantage in regard to Retrofit’s interceptor is, that you can add multiple of them and not just one.

OkHttpClient.Builder httpBuilder = new OkHttpClient.Builder();

// add interceptors
httpBuilder.addInterceptor(/** Your Interceptor **/)  
httpBuilder.addNetworkInterceptor(/** Your Network Interceptor **/)  
httpBuilder.authenticator(/** Your Authenticator **/)

// create the OkHttpClient instance from httpBuilder
OkHttpClient client = httpBuilder.build();

RestAdapter.Builder builder = new RestAdapter.Builder()  
        .setClient(new Ok3Client(client))
        .setEndpoint(API_BASE_URL);

We hope that helps to understand how adding OkHttp 3 to your Retrofit 1 project can improve your project’s quality and enables new powerful possibilites (OkHttp 3 interceptors are really powerful!) with the advantages of OkHttp 3.


Additional Resources


Explore the Library

Find interesting tutorials and solutions for your problems.

Miscellaneous