Mobile Development 15 min read

Designing a Network Framework from Scratch and Analyzing OkHttp & Retrofit Implementations

This article explains how to design a network framework from the ground up, explores the three‑layer understanding of frameworks, details the core components, thread scheduling, request/response processing chains, and then deep‑dives into OkHttp and Retrofit implementations, their limitations, and extensibility techniques.

Beike Product & Technology
Beike Product & Technology
Beike Product & Technology
Designing a Network Framework from Scratch and Analyzing OkHttp & Retrofit Implementations

The author introduces a three‑layer model for mastering a framework: first learning how to use it, then understanding its source code, and finally grasping why it exists and how it could be improved.

To reach the deepest layer, the author suggests discarding the existing framework and building a network framework from zero, then reflecting on its purpose and design.

The basic network framework consists of three parts: a Request class that encapsulates URL, method, headers and body; a Response class that holds the server’s reply; and a requestor that converts a Request into a Response .

Because network calls are time‑consuming and may run concurrently, a thread‑pool is introduced to reuse threads and schedule tasks.

Beyond simple request/response, real projects need additional processing such as adding device info, encryption, or decryption. The author proposes a chain of processors (request and response processors) that form a responsibility‑chain pattern, where each processor can transform the data and the chain can be symmetric around the requestor.

OkHttp’s asynchronous flow is then dissected: a Call is created via OkHttpClient.newCall(request) , enqueued to a dispatcher’s thread pool, and processed through a dynamically built interceptor chain (custom, built‑in, network, and the final CallServerInterceptor ). After the response is obtained, a callback delivers it on a background thread.

Three shortcomings of raw OkHttp are highlighted: verbose request construction, callbacks occurring on background threads requiring manual thread‑switching, and the need for manual JSON‑to‑Bean conversion. Retrofit addresses these issues.

Retrofit lets developers declare an interface with annotations, e.g.: @GET("/generalPage") Call getMyData(@Query("ID") String id, @Query("type") String type); DemoAPI demoAPI = retrofit.create(DemoAPI.class); Call call = demoAPI.getMyData(id, type); Retrofit generates a dynamic proxy that creates an OkHttpCall , wraps it with an ExecutorCallbackCall to handle thread switching, and finally returns a typed Call object.

Extensions are discussed: adding custom interceptors, implementing custom CallAdapter s and Converter s, and using custom annotations. Since Retrofit 2.5, the Invocation tag can be attached to a request, allowing interceptors to retrieve the original method and arguments: return requestBuilder.get().tag(Invocation.class, new Invocation(method, argumentList)).build(); In an interceptor: Invocation invocation = request.tag(Invocation.class); // use invocation.method() and invocation.arguments()

To handle scenarios where a synchronous interceptor needs asynchronous work (e.g., prompting a user for face recognition), the author suggests converting OkHttp’s synchronous chain into an asynchronous callback chain, replacing the blocking Response proceed(Request) with a non‑blocking callback.

In conclusion, OkHttp and Retrofit form a mature foundation for Android networking; developers should stay curious, apply what they learn, and continuously iterate their understanding in new business contexts.

design patternsAndroidInterceptorthreadingOkHttpNetwork FrameworkRetrofit
Beike Product & Technology
Written by

Beike Product & Technology

As Beike's official product and technology account, we are committed to building a platform for sharing Beike's product and technology insights, targeting internet/O2O developers and product professionals. We share high-quality original articles, tech salon events, and recruitment information weekly. Welcome to follow us.

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.