OkHttp: A Smarter HTTP Client for Your App
OkHttp is a smart, resilient HTTP client for Android and Java. It automatically handles complexities like connection pooling, caching, and GZIP compression to make requests faster and more reliable.
Why it exists
Modern applications exchange data and media over HTTP. Doing this efficiently makes your app feel faster and saves user bandwidth. OkHttp was created to be an HTTP client that is efficient, secure, and resilient by default, handling network complexities automatically.
The mental model
Think of OkHttp as a smart, well-behaved user agent (like a modern web browser) for your application's networking layer. It handles the complex, low-level details of making fast, reliable, and secure network requests so you don't have to.
How it works
OkHttp improves performance and reliability through several built-in mechanisms. First, it supports HTTP/2, allowing multiple requests to the same host to share a single socket. If HTTP/2 isn't available, it uses connection pooling to reduce latency. Second, it transparently uses GZIP to shrink download sizes and caches responses to avoid unnecessary network calls for repeat requests. Third, it perseveres through network issues by silently recovering from common connection problems and trying alternate IP addresses if a service provides them. It also supports modern security features like TLS 1.3.
When to use it
Use OkHttp in any Android (API 21+) or Java 8+ application that needs to make HTTP requests. It's a robust, general-purpose client suitable for fetching data from APIs, downloading files, and posting data to servers. Its fluent, immutable request/response API makes it easy to use for both synchronous blocking calls and asynchronous calls with callbacks.
When not to use it
OkHttp is not the right tool if you need to violate HTTP specifications to communicate with a non-compliant server. For example, it strictly disallows sending a GET request with a body. If you require extensive customizability to send invalid requests, you'll need a different library. Also, the core library is not for very old Android (pre-5.0) or Java (pre-8.0) projects, though an older, unsupported branch exists.
One canonical example
To fetch data from a URL, you first create a single, shared instance of OkHttpClient. Then, you build a Request object using its fluent builder, specifying the URL. You use the client to create a newCall with your request and then execute it. This returns a Response object, from which you can access the response body, headers, and status code. The basic flow looks like: client.newCall(new Request.Builder().url(someUrl).build()).execute().
Interview question
Which of the following best explains why OkHttp is considered a "smarter" and "resilient" HTTP client?
- a.It provides a built-in mechanism for violating HTTP specifications for non-compliant servers.
- b.It automatically manages complexities like connection pooling, caching, and GZIP compression.Correct
- c.It requires explicit developer implementation for handling network retries and alternate IP addresses.
- d.It allows developers to manually configure low-level network protocols for maximum control.
Why? this is the answer
OkHttp is considered smart and resilient because it automatically handles complex network details such as connection pooling, caching, and GZIP compression, as well as silently recovering from network issues. It does not require manual configuration of these low-level aspects, nor does it allow violating HTTP specifications.
Just read this? Test yourself on what you have been reading.
Read the original → square.github.io
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.
See open roles