Retrofit uses a cosmetically pattern-based unified invocation to create network request interface instances and network request parameter configurations as follows:
Dynamically create an instance of the network request interface (Proxy mode – Dynamic proxy) Create a serviceMethod object (Builder mode & singleton mode (caching mechanism)) Configure the network request parameters for a serviceMethod object: The URL address, network request executor, network request adapter & data converter of the corresponding network request is obtained from Retrofit objects by parsing the parameters, return values, and annotation types of the network request interface method. (Policy mode) A thread switch is added to the serviceMethod object so that it can be switched from the child thread to the main thread after receiving the data and processed with the result of the returned data (decoration mode). Finally, an OkHttpCall network request object is created and returned
Retrofit uses OkHttp by default, the OkHttpCall class (which implements retroFIT2. Call interface), but you can customize the Call class you want
OkHttpCall provides two network request modes: synchronous request: okHttpCall.execute () Asynchronous request: okHttpCall.enQueue () These two network request modes are described in detail below.
OkHttpCall enqueue () and execute () will not be analyzed here
Step 1: Use the corresponding ParameterHandler to parse each parameter in the network Request interface method, and then create an OkHttp Request object based on the ServiceMethod object. Step 2: Use the OkHttp Request to send network requests. Step 3: Parse the returned data using the previously set data Converter (GsonConverterFactory) to get a Response object