okhttp
Square’s meticulous HTTP client for the JVM, Android, and GraalVM.
OkHttp is a networking library for Java, Kotlin, and Android apps that makes HTTP requests cleaner and faster, with automatic connection pooling, response caching, compression, and modern TLS security built in.
OkHttp is a library for making HTTP network requests from Java, Kotlin, and Android applications. HTTP is the protocol used to transfer data over the web — whenever your app fetches data from an API, downloads an image, or posts a form, it makes an HTTP request. The standard networking tools built into the Java and Android platforms work but have awkward APIs and lack many modern features. OkHttp provides a cleaner, more capable alternative.
It is efficient by default in several important ways. It supports HTTP/2, a newer version of the protocol that allows multiple requests to the same server to share a single connection instead of opening a new one for each request, which significantly reduces latency. It uses connection pooling even for older HTTP/1.1 connections, automatically compresses responses using GZIP to reduce download sizes, and caches responses so repeat requests for the same data skip the network entirely. It handles real-world network reliability: if a server has multiple IP addresses (common for load-balanced services), OkHttp tries them in order on failure. It supports modern TLS security features including TLS 1.3 and certificate pinning. The API uses a builder pattern common in Java/Kotlin where you chain method calls to configure a request before sending it, and it supports both synchronous (blocking) and asynchronous (callback-based) request modes.
You would use OkHttp in any Java or Kotlin application — including Android apps — that makes HTTP calls to web APIs, downloads files, or communicates with a backend server. It is the default HTTP client underlying many other popular Android libraries. It also includes a MockWebServer module for writing tests that simulate a real HTTP server without making actual network calls. The library is written in Kotlin and distributed via Maven Central.
Where it fits
- Fetch data from a REST API in an Android or Kotlin app with automatic retry, caching, and efficient HTTP/2 connections.
- Download files in a Java or Android app with GZIP compression reducing bandwidth and response caching skipping repeat downloads.
- Write unit tests that simulate HTTP server responses without real network calls using the MockWebServer module.
- Communicate securely with a backend API using certificate pinning to prevent man-in-the-middle attacks.