Skip to content
Go back

OkHttp vs Retrofit — How Android Networking Works Internally

OkHttp vs Retrofit — How Android Networking Works Internally

Every Android network call eventually goes through OkHttp. Whether you use Retrofit, Volley, or even the old HttpUrlConnection, OkHttp is the engine underneath. Understanding the distinction between the engine and the wrapper clarifies a lot about how Android networking libraries work and why they are designed the way they are.


The HTTP Engine History

Android networking has gone through two generations:

1. HttpUrlConnection (Apache) — the original

Before Android 4.4, HttpUrlConnection was the standard HTTP client. Writing a single network call required manually creating a connection, setting headers, reading an input stream byte by byte, handling errors, and closing the connection. Libraries like Volley were built on top of it to hide that complexity.

2. OkHttp (Square) — Android 4.4+

OkHttp replaced HttpUrlConnection as the HTTP engine. It handles DNS resolution, TLS handshakes, connection pooling, redirects, and compression transparently. Starting with Android 4.4, even if you write HttpUrlConnection code, Android internally routes it through OkHttp.


OkHttp: The HTTP Engine

OkHttp is the component that actually makes the network call. It:

OkHttp is a complete HTTP client on its own. You can use it without Retrofit. Writing a call directly looks like this:

val client = OkHttpClient()
val request = Request.Builder().url("https://api.example.com/users").build()
val response = client.newCall(request).execute()
val body = response.body?.string()

This works, but for a real application with many endpoints, different response types, and background threading, you would write this setup for every single call — and manage thread switching yourself. That is where Retrofit comes in.


Retrofit: A Wrapper on Top of OkHttp

Retrofit does not make network calls itself. It delegates to OkHttp. What it adds is an abstraction layer that turns an annotated Kotlin interface into a fully functional HTTP client.

interface ApiService {
    @GET("users")
    suspend fun getUsers(): Response<List<User>>

    @POST("users")
    suspend fun createUser(@Body user: User): Response<User>
}

Retrofit generates all the OkHttp boilerplate behind this interface at runtime. A call that would take 50+ lines of OkHttp code becomes a single function with annotations.

Retrofit provides three things that OkHttp does not:

1. Adapter pattern for threading frameworks

Retrofit can integrate with coroutines (suspend functions), Kotlin Flow, RxJava, or a raw thread pool. Switching the threading model is a matter of changing the call adapter factory — the business logic stays unchanged.

2. Automatic JSON parsing

Retrofit supports converter factories. When a response arrives, the converter deserializes the JSON body into your data class automatically. You never touch JSONObject or BufferedReader.

3. Type-safe interface

The API definition is a Kotlin interface. Types are checked at compile time. If the response type changes, the compiler catches it.

The relationship is the same as Volley had with HttpUrlConnection: Volley wrapped HttpUrlConnection to add threading and parsing. Retrofit wraps OkHttp to do the same at a higher level of abstraction.


Gson vs Moshi — Why Moshi Is Faster

Both Gson and Moshi are JSON converter libraries that work with Retrofit. The difference is in how they discover the fields of your data class.

Gson uses reflection. At runtime, it inspects the class using the Java reflection API, reads field names and types dynamically, and maps JSON keys to fields. Reflection scans the class structure, performs string operations, and executes runtime lookups — it adds overhead on every parse.

Moshi uses code generation. The @JsonClass(generateAdapter = true) annotation triggers the Moshi Kotlin codegen plugin to generate a dedicated adapter class at compile time. At runtime, no reflection is needed — the adapter knows exactly which fields exist and how to map them. The result is faster parsing with less memory pressure.

// Gson — no annotation needed, works via reflection
data class User(val name: String, val age: Int)

// Moshi — codegen generates a type-safe adapter at compile time
@JsonClass(generateAdapter = true)
data class User(val name: String, val age: Int)

For new Kotlin projects, Moshi is the better choice. For legacy code already using Gson, the difference is measurable but not always worth the migration cost.


The Full Stack

When your app calls apiService.getUsers():

  1. Retrofit receives the call, checks the adapter (coroutine/Flow/RxJava), applies the converter factory.
  2. Retrofit builds an OkHttp Request object from the annotations.
  3. OkHttp handles the actual HTTP work: DNS, TLS, connection pool lookup, request send, response read.
  4. OkHttp returns the raw response to Retrofit.
  5. Retrofit passes the response body through the converter (Gson/Moshi) and returns a typed object to your code.

OkHttp handles the network. Retrofit handles the translation between Kotlin types and HTTP.


Summary

OkHttpRetrofit
RoleHTTP engine — makes the actual callWrapper — adds abstraction over OkHttp
ThreadingManualAdapter pattern — coroutines, Flow, RxJava
JSON parsingManualAutomatic via converter factory
API definitionProgrammatic (request builders)Declarative (annotated interface)
Use directly?Yes, for low-level controlYes, for most Android apps

Retrofit without OkHttp cannot exist — every Retrofit request goes through OkHttp underneath.


Share this post on:

Previous Post
HTTP Methods Explained — HEAD, POST, PUT, and PATCH
Next Post
Android Starter Pattern and Adapter Click Listener Best Practice