Note: Please combine other documents and read the source code to read

1. The retrofit is what

To be precise, Retrofit is an encapsulation of a RESTful HTTP Web request framework. Reason: The web request work is essentially done by OkHttp, while Retrofit is only responsible for encapsulating the web request interface

  • App applications make requests through the Retrofit network, essentially encapsulating the request parameters, headers, urls, etc., using the Retrofit interface layer, and then OkHttp does the rest of the request.
  • After the server returns the data, OkHttp gives the original results to Retrofit, which parses the results according to the user’s needs.

advantage

  1. Adaptation of network request parameters, protocols, forms, Restful
  2. Encapsulate OkHttp, Call
  3. Data result encapsulation

2. The retrofit process

3. Proxy instance creation process

retrofit = new Retrofit.Builder()
         .baseUrl("")
         .addConverterFactory(GsonConverterFactory.create(new Gson()))
         .build();
IShareListService shareListService = retrofit.create(IShareListService.class);
Copy the code

1: Criteria for successfully creating a Retrofit object: configuring member variables in a Retrofit class

  • BaseUrl: indicates the URL of the network request
  • CallFactory: Network request factory
  • CallbackExecutor: Callback method executor
  • AdapterFactories: collection of network request adapterFactories
  • ConverterFactories: a collection of data converterFactories

2: Creates an ISharedListService interface object. The create function uses a dynamic proxy to create the interface object. This design allows all access requests to be proxy

Call sharedListCall = sharedListService.getSharedList(2.1);
Copy the code

When you call getSharedList, inside the dynamic proxy there is a function getSharedList that calls Invoke, which is the invoke function from RetroFit. Therefore, dynamic proxy can proxy all interfaces and make all interfaces invoke functions, so that the execution of the calling function can be intercepted and the parameter configuration of the network interface can be normalized.

4. Access the interface creation process

5. Network request process

converter response2 javaBean

6. Design patterns in Retrofit

  1. Retrofit instances are built from the Builder class using the Builder pattern. The builder design pattern can be used when the constructor takes more than four parameters and has an optional number of parameters
  2. Retrofit was created as a callFactory, which uses a factory approach to the design pattern, but does not seem intended to support other factories.
  3. The whole Retrofit is based on a time-looking pattern. Unified calls to create network request interface instances and network request parameter configurations
  4. Retrofit uses dynamic proxies to create network request interface instances. This is the biggest reuse of Retrofit for users, and the rest of the code is designed to support this dynamic proxy for user convenience
  5. The policy pattern is used to configure network request parameters for serviceMethod objects, that is, to obtain the corresponding network URL address, network request executor, network request adaptor and data converter from Retrofit objects by parsing the parameters, return values and annotation types of network request interface methods
  6. ExecuteCallBack uses decorator mode to wrap the callbackExecutor for thread switching
  7. ExecutorCallbackCall uses static proxies (proxies) to proxy Call for network requests. The real network requests are handled by okhttpCall, but okhttpCall does not perform them itself. It is the only portal for OKHTTP to provide calls to the outside world (Retrofit), which is actually the facade mode
  8. ExecutorCallbackCall initialized is inside the ExecutorCallAdapterFactory through the adapter pattern is created. The CallAdapter uses the adapter pattern to provide services for creating access to the Call interface. Default don’t use the default ExecutorCallAdapterFactory will add Rxjava okhttp3. Call into retroift the call, if there are Rxjava will okhttp3. Call into abservable.