Understanding the retrofit framework from the perspective of ServiceMethod

For Android developers, retrofit can be said to be a more practical network request framework, and it is open source. Then, from the perspective of request execution, starting with the method we defined in the interface, interpret the execution process of retrofit. The purpose is to give us a framework understanding of the execution process of retrofit, and at the same time, during the interview, we can "talk" with the interviewer.

What is ServiceMethod? The core idea of ​​retrofit is to abstract the http request process into an object ServiceMethod. When this object is constructed, a method object will be passed in through java reflection, and this object is the request method we defined in the interface. By finding usage on the method object, we can find that the method object is used in two places.
As you can see, two key information of the method object are used: annotation and return type. The two methods involved appear in ServiceMethod.build() at the same time.

    public ServiceMethod build() {
      callAdapter = createCallAdapter();
      responseConverter = createResponseConverter();
    }


Construct createCallAdapter and responseConverter through the method's returnType, and then automatically complete the conversion from the server's return result to the program's model class instance. Define network request related parameters through annotation. The parsing of annotations is a simple but tedious task, and each annotation needs to be judged one by one. Therefore we focus on the analysis of callAdapter and responseConverter. In the process of reading the relevant code, I found that the generic part of it has caused a great obstacle to our understanding of the entire framework. Therefore, if you want to understand retrofit, you must understand the meaning of each of its generic classes, or you will be confused when you look at it.

Among them, getAlbums() is our method method. According to this method, a ServiceMethod object is generated, and the returned result is serviceMethod.callAdapter.adapt(okHttpCall); . And we know that the return type of getAlbums() defined in the interface happens to be RadioCall<RadioAlbumModel>, and the two types are just right.

Inside radioCall.execute(), we will call delegates.execute() to actually execute the network request. The type of delegates is retrofit.OkHttpCall. Then, initiate a request, wait for the server to return the result, and process the result. Shangxuetang • Baizhan programmer Mr. Chen pointed out that the result at this time is still rawResponse, that is, it is a json string, not a java model object that can be used directly. At this time, we need responseConverter to help us convert.

As we said above, RadioCall generally takes an okHttpCall as a parameter of the constructor, and then delegates the actual request to okHttpCall, and then the Response<R> in the above code can be obtained in the onResposne callback, bringing our class into , which is Response<RadioAlbumModel>. And this is the execution result of execute(). Then we can get the model object in Response<RadioAlbumModel>.

At this point, the request execution process of retrofit is analyzed. Finally, we will explain the entire implementation process from a macro perspective and from an interview perspective.

First, we call the interface method through the retrofit instance we created. All interface methods will call invoke in an anonymous class new InvocationHandler under the action of the java dynamic proxy mechanism. In invoke, a serviceMethod will be constructed according to the method we want to call, and then serviceMethod.callAdapter.adapt(okHttpCall) will be called as the return result.

When constructing a serviceMethod, a converter and a callAdapter are constructed according to the return type of the method in the interface. Among them, the converter generally uses the gson converter. The gson converter can automatically convert the json data returned by the server into an instance of the model class in java. The vast majority of the implementation of callAdapter is to receive an okHttpCall instance in the constructor, and then delegate enqueue and execute to this okHttpCall instance for execution. After okHttpCall gets the server data, it will use serviceMethod.toResponse(body) to transform the data. Among them, converter is used when converting. After the data conversion is completed, it is encapsulated into Response<R> and passed to the caller. where R is our data class.


 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325510889&siteId=291194637