There is no doubt that OkHttp has put other web request frameworks in the shade. It is an efficient HTTP client that is the most widely used web framework for Android development today. I have to say OkHttp is really powerful and really sweet.

However, in the actual project development process, this framework also leaves many problems for developers:

  • It is not suitable for automatic thread switching, so the message back needs to be cut to the main thread, which has to write itself.
  • The calls are complex and need to be wrapped themselves.
  • The interface configuration of user network request is tedious, especially when the request body, request header and parameters need to be configured.
  • The data parsing process requires the user to manually get responsBody for parsing, which cannot be reused;
  • Cache failure: Network requests usually obtain some hardware or network information about the phone, such as the network environment used. Requests may also be encrypted for security of information transmission. In these cases OkHttp’s cache system becomes ineffective, causing users to be unable to access the cache without a network.

But Retrofit really solves these problems by packaging on top of OkHttp, making it easier for us to develop web frameworks.

What does Retrofit do with OkHttp

Retrofit is a very powerful encapsulation framework that can be configured with different HTTP clients to implement network requests, such as OKHTTP, HttpClient, etc. Parameter annotations for request methods can be customized; RxJava can be synchronous or asynchronous; Easy to achieve super decoupling; Configure different anti-sequence tools to parse data, and there are many design patterns, very convenient and flexible to use:

Source code flow chart for Retrofit compiled by Stay

The web request work is still essentially done by OkHttp, Retrofit encapsulates the web request interface, and does not change the nature of the web request because OkHttp is powerful enough. Retrofit’s encapsulation makes things a lot easier. There are very rich design patterns involved in configuring requests directly through annotations, different HTTP clients can be used, although HTTP is the default, different Json Converters can be used to serialize data, and RxJava is supported.

Retrofit + OkHttp + RxJava is one of the most popular frameworks. But there’s a certain threshold, especially when it comes to mastering Retrofit’s core principles. Like OkHttp, Retrofit is now one of the must-ask questions for big factory interviews:

Many interviewers also throw a punch line at this part:

  • How does Retrofit work
  • What network loading libraries have you used? Retrofit core implementation principles?
  • If you were asked to implement some of Retrofit’s core features, how would you do it?
  • How are Retrofit annotations interpreted?
  • What does Retrofit use for the network request layer?
  • What design patterns are used in Retrofit?
  • What encapsulation does Retrofit do on OkHttp? How is the difference between dynamic and static proxies implemented?
  • How does the Android development Repository layer get data back from RetroFit?
  • Is it feasible to do Retrofit with APT? Why use dynamic proxy instead of APT?

How do I quickly dive into Retrofit’s source layer principles?

Source Code Analysis of Retrofit Network Implementation and OKhttp Implementation Principle

Android 100 framework source code analysis

“From OkHttp Source Code interpretation to Handwritten Web Access Framework”

The above video has the Android network framework to explain, you can go to reference learning.

“Android 100 big framework source code analysis” knowledge summary has been sorted out on GitHub, you can click on the small card below for reference.