OkHttp for Java: Next Generation HTTP Client
\n\n1. What It Is and Why You Need It
\n\nOkHttp is a modern, efficient HTTP library for Java and Android, developed by Square. It has become the de facto standard for making network requests in the Android ecosystem and is actively used in server-side Java applications. OkHttp provides a clean, fluent API for sending HTTP requests and receiving responses, supporting all modern protocol features: HTTP/2, WebSockets, GZIP compression, response caching, and automatic connection recovery on failures.
\n\nWhy do you need OkHttp if there is the standard java.net.HttpURLConnection? First, OkHttp is much easier to use — instead of cumbersome constructions with connection setup, you simply create a request and execute it. Second, the library solves many problems "out of the box": it automatically handles redirects, manages a connection pool for reuse, supports transparent GZIP compression, and can cache responses to save bandwidth. Third, OkHttp is the foundation of popular libraries such as Retrofit and Picasso, making it indispensable for Android developers.
OkHttp is ideal for any Java project that requires interaction with REST APIs, file uploads, working with WebSockets, or simply making HTTP requests. Thanks to HTTP/2 support, the library allows multiplexing requests over a single connection, significantly speeding up work with modern servers. Version 4.x and above has been completely rewritten in Kotlin but maintains full backward compatibility with Java code, making it accessible to all Java developers.
\n\n2. Installation
\n\nOkHttp is distributed via Maven Central. To add the library to your project, add the dependency to the build.gradle file (for Gradle) or pom.xml (for Maven).
Gradle (Android / Java project)
\ndependencies {\n implementation 'com.squareup.okhttp3:okhttp:4.12.0'\n // For request logging (optional)\n implementation 'com.squareup.okhttp3:logging-interceptor:4.12.0'\n}\n\nMaven
\n<dependency>\n <groupId>com.squareup.okhttp3</groupId>\n <artifactId>okhttp</artifactId>\n <version>4.12.0</version>\n</dependency>\n\nFor Java modules (without Gradle/Maven)
\nDownload the JAR file from Maven Central and add it to your project's classpath.
\n\nImportant: OkHttp 4.x requires Java 8+ or Android API 21+. For older versions, use OkHttp 3.x.
\n\n3. Quick Start — Minimal Working Example
\n\nThe simplest example — making a GET request and printing the response body to the console. Create a class Main.java and copy the following code:
import okhttp3.OkHttpClient;\nimport okhttp3.Request;\nimport okhttp3.Response;\nimport java.io.IOException;\n\npublic class Main {\n public static void main(String[] args) {\n // 1. Create an HTTP client (usually one per application)\n OkHttpClient client = new OkHttpClient();\n\n // 2. Build the request\n Request request = new Request.Builder()\n .url("https://api.github.com/users/octocat")\n .get()\n .build();\n\n // 3. Execute the request synchronously (blocks the thread)\n try (Response response = client.newCall(request).execute()) {\n if (response.isSuccessful()) {\n System.out.println("Response: " + response.body().string());\n } else {\n System.err.println("Error: " + response.code());\n }\n } catch (IOException e) {\n e.printStackTrace();\n }