Square’s new HTTP client for Android and Java.
Written by Jesse Wilson.
Android has two HTTP clients built-in:
- Apache HTTP Client is stable yet basic.
- HttpURLConnection supports advanced features, but suffers a few annoying bugs on older devices.
At Square, we want it all: advanced features that work right on every device. We also want to ship new tech without waiting for new Android releases.
OkHttp is our new open source HTTP client for Android and Java. Our goal is to build the most robust, most efficient HTTP client.
Flaky networks are a fact of life, especially on mobile. OkHttp employs a few strategies to automatically recover should a connection fail.
For example, if your web service is hosted in multiple data centers for redundancy, OkHttp will attempt each IP address until it finds one that works. It also recovers from failed TLS handshakes and troublesome proxy servers.
The code is based on the latest version of Android’s HttpURLConnection, which has been battle-tested against real-world web servers.
The fastest download is the one you don’t have to make. OkHttp includes an integrated response cache. Unlike an application-level cache, the integrated cache uses conditional requests to extend the life of stale resources.
When OkHttp does hit the network, it uses transparent response compression and a sophisticated connection pool. These on-by-default optimizations save bandwidth and reduce latency.
My favorite feature is the SPDY backend. This is Google’s efficient new wire transport layer that is the starting point for the new HTTP 2.0 draft. SPDY enables multiple concurrent requests to share a single socket. This lowers the cost of HTTPS so you no longer have to choose between security and performance.
Give it a try!
If you’ve written an Android app, OkHttp may improve the speed & reliability of your networking. It supports both the HttpURLConnection and HttpClient APIs so you can upgrade painlessly.
If you’re writing a new app, the project website has code examples to get you started.
Get OkHttp from Maven or download it from the project website.