Mobile Development 19 min read

Understanding Network Protocols, OkHttp Architecture, and Kotlin Reflection for Offline Upload in Android

This article explains the fundamentals of TCP/UDP and HTTP, details OkHttp's interceptor chain and caching mechanisms, and demonstrates a generic offline‑upload solution using Kotlin reflection to persist and replay network calls when connectivity is restored.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
Understanding Network Protocols, OkHttp Architecture, and Kotlin Reflection for Offline Upload in Android

In Android development, network requests rely on fundamental transmission protocols such as TCP, UDP, and HTTP; mastering these basics is essential before using higher‑level libraries.

The article explains the OSI‑like four‑layer model, the differences between TCP (reliable, connection‑oriented) and UDP (unreliable, connection‑less), and the structure of HTTP request and response messages, including methods, status lines, and version differences (HTTP/1.0, 1.1, 2.0).

It then introduces OkHttp, describing how the library builds HTTP messages, establishes TCP connections, and employs a chain of interceptors to process requests and responses. The interceptors covered are:

Application interceptor – adds headers, encrypts parameters, can abort the call.

RetryAndFollowUpInterceptor – handles retries, redirects and connection reuse.

BridgeInterceptor – adds fixed headers, handles cookies and gzip compression.

CacheInterceptor – implements HTTP caching based on Cache‑Control, ETag, and Last‑Modified.

ConnectInterceptor – manages the low‑level socket and stream allocation.

NetworkInterceptor – runs just before the network call, useful for token injection.

CallServerInterceptor – writes the request line, headers, body and reads the response.

The article also details how OkHttp supports chunked transfer encoding for dynamic request bodies and how to configure caching with OkHttpClient.Builder().cache(new Cache(new File("cache"), 10 * 1024 * 1024)) .

Finally, a generic offline‑upload solution is presented that uses Kotlin reflection to persist a method call (class name, method name, parameters, and optional callback) when the network is unavailable, and later re‑executes it once connectivity is restored. The core manager stores upload tasks in MMKV, schedules periodic checks, and reconstructs the call via Class.forName(className).kotlin and method.callSuspendBy(...) . Example code snippets illustrate the manager, cache handling, and the upload invocation.

The conclusion summarises best practices: prefer HTTP/2, leverage OkHttp’s application interceptors, enable gzip compression, configure HTTP caching, use network interceptors for token handling, and adopt chunked transfer for large or dynamically generated payloads.

AndroidreflectionnetworkKotlinOkHttpOfflineUpload
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.