What is OkHttp and why do you need it?
\n\nOkHttp is a powerful and efficient HTTP library for JVM (Java Virtual Machine), Android, and Kotlin/Native, developed by Square. It provides a convenient API for making HTTP requests and processing responses. OkHttp is the de facto standard for networking in the Android ecosystem and is actively used in server-side Kotlin applications.
\n\nWhy use OkHttp instead of standard Java tools (HttpURLConnection)?
\n- \n
- Simplicity and convenience: A clean, fluent API that is easy to read and write. \n
- Performance: Built-in support for HTTP/2, automatic response compression (gzip), connection pooling, and caching. \n
- Reliability: Automatic request retries on network failures, resilience to DNS and TLS errors. \n
- Flexibility: Easily configurable interceptors for logging, authentication, modifying requests and responses. \n
- Testability: Ability to mock the network layer using MockWebServer. \n
OkHttp is ideal for Android applications, REST API clients, microservices, and any other Kotlin projects that require reliable and fast HTTP communication.
\n\nInstallation
\n\nOkHttp is distributed via Maven Central. To add it to your project, use the Gradle build system (most common for Kotlin).
\n\nFor Android / Kotlin/JVM project (build.gradle.kts):
\ndependencies {\n implementation("com.squareup.okhttp3:okhttp:4.12.0")\n // For logging (optional)\n implementation("com.squareup.okhttp3:logging-interceptor:4.12.0")\n}\n\nFor Android / Kotlin/JVM project (build.gradle):
\ndependencies {\n implementation 'com.squareup.okhttp3:okhttp:4.12.0'\n implementation 'com.squareup.okhttp3:logging-interceptor:4.12.0'\n}\n\nFor Maven (pom.xml):
\n<dependency>\n <groupId>com.squareup.okhttp3</groupId>\n <artifactId>okhttp</artifactId>\n <version>4.12.0</version>\n</dependency>\n\nImportant: Make sure the OkHttp version is compatible with your Kotlin version. The latest version can always be found on MVNRepository.
\n\nQuick Start
\n\nThe simplest example is to make a GET request and get the response body as a string.
\n\nimport okhttp3.OkHttpClient\nimport okhttp3.Request\nimport java.io.IOException\n\nfun main() {\n // 1. Create a client (usually one per application)\n val client = OkHttpClient()\n\n // 2. Build the request\n val request = Request.Builder()\n .url("https://api.github.com/users/octocat")\n .build()\n\n // 3. Execute the request synchronously (blocks the current thread)\n client.newCall(request).execute().use { response ->\n if (!response.isSuccessful) {\n throw IOException("Unexpected code $response")\n }\n // 4. Get the response body\n println(response.body?.string())\n }\n}\n\nExplanation:
\n- \n
OkHttpClient()— creates a client instance. It is thread-safe, so it is recommended to create one instance and reuse it. \n Request.Builder()— uses the Builder pattern to configure the request. \n client.newCall(request)— creates a new Call, which can be executed synchronously (execute()) or asynchronously (enqueue()). \n response.use { ... }— ensures that response resources (body, connection) are closed after use. \n response.body?.string