I made some notes when I combed the retrofit source code, but it was written a long time ago. I felt that the analysis was not smooth enough and made a lot of leaps
At that time was taking these problems to see the source code
1. What is the process of the request? 2. How is the underlying request made with OkHttp? Okhttp is asynchronous. How did Retrofit help us switch to the main thread? 4. When and how are annotations parsed? 5. What are the functions of Converter and CallAdapter? 6. How to support Kotlin coroutine suspend function?
A simple flowchart for Retrofit
Start with retrofit.build ()
Point 1
- Because we don’t normally pass a callbackExecutor, it goes in
Platform. The defaultCallbackExecutor () statement
But if we go straight in, there’s nothing there
- Platform = platform = platform = platform = platform = platform = platform = platform
- Then it goes to The Android() platform, so it can be assumed here
callbackExecutor == MainThreadExecutor
Used to switch threads
Point 2
- You can see that the callbackExecutor from the previous step is passed in as a parameter
platform.defaultCallAdapterFactories(callbackExecutor)
Continue to follow up as you can see
- Then enter the
DefaultCallAdapterFactory
- When entering
ExecutorCallbackCall
- This is how Retrofit helps us switch to the main thread after making a web request, which is basically at
build
Method, throughPlatform
A subclass ofAndroid()
Added a default Executor:MainThreadExecutor
“And then added anotherDefaultCallAdapterFactory
And created anonymous inner classesCallAdapter
willMainThreadExecutor
, packaged inExecutorCallbackCall
Request a callback in the network, switch threads through it, and throw the result out
Focus on three
- If you don’t add ConverterFacTory, this is the default way to convert data, or you can customize your own data conversion by inheriting Converter. That’s what Gson’s does.
Then Retrofit. The create ()
The method looks simple enough to get an instance of the interface through the dynamic proxy, and then you can call the method. When we call the method through the instance, we go to InvocationHandler.invoke
Let’s take a look at ServiceMethod
- Enter the
loadServiceMethod
, the method is very simple, is the first to save from their own search, no generation and then save, later can reuse.
- Method return value
ServiceMethod
What is the entry to see
- Here we continue
RequestFactory
, which are method annotation processing. Note the adaptation to the Kotlin coroutineIn the build method of the RequestFactory
Will go through all the parameters to parse them
- In follow up to
parseParameter
We know that Kotlin adds one to the end of the method at compile timeContinuation<T>
The parameters of the
- And this flag bit will be saved in
requestFactory
Then pass in HttpServiceMethod!!!!! ResponseType <T> responseType <T> responseType <T> responseType <T>
Normal method of access to callbored(Wrong, it should beloadServiceMethod(method).invoke()
Call)
Suspend method
Interface. The enqueue by Http
When the apiService. GetHttp () interface method is called to make an Http request
Invocationhan.invoke ()
LoadServiceMethod (method).invoke(
– so DafalutCallAdapterFactory again
- Look again at the OkHttpCall method
- This is the actual Call to create OkHttp
- Once you get the call from Okhttp, you can make an online request