retrofit2 of Android jar / library source code analysis

catalog: Source code analysis of andorid jar / Library 

Retrofit2:

Function:

The okhttp library is encapsulated to communicate with the web, and the interface address is called by dynamic proxy, and the result is assigned by callback.

Chestnut:

Define an interface for access usage.

public interface IServiceApi {
    @FormUrlEncoded
    @POST("login")
    Call<LoginResult> login(@Field("name") String name, @Field("pwd") String pwd);

    @GET("getinfo")
    Call<UserInfo> getinfo(@Query("token") String token);

    @GET("getinfo2")
    Call<UserInfo> getinfo2(@Query("token") String token);
}

Calling interface 1. can be invoked in main because it is executed asynchronously (enqueue).

Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl("http://192.168.86.11:8087/").build();
        IServiceApi api = retrofit.create(IServiceApi.class);
        Call<LoginResult> call = api.login("test", "test1234");
        call.enqueue(new Callback<LoginResult>() {
            @Override
            public void onResponse(Call<LoginResult> call, Response<LoginResult> response) {
                LoginResult loginResult = response.body();
                if(loginResult != null) {
                }
            }

            @Override
            public void onFailure(Call<LoginResult> call, Throwable t) {
                Log.i(tag, "ex " + t.getMessage());
            }
        });

Calling interface 2. can be invoked in main because it is executed asynchronously (enqueue).

Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl("http://192.168.86.11:8087/").build();
        IServiceApi api = retrofit.create(IServiceApi.class);
        Call<UserInfo> call = api.getinfo("testtesttest");
        call.enqueue(new Callback<UserInfo>() {
            @Override
            public void onResponse(Call<UserInfo> call, Response<UserInfo> response) {
                UserInfo userInfo = response.body();
                if(userInfo != null) {
                }
            }

            @Override
            public void onFailure(Call<UserInfo> call, Throwable t) {
                Log.i(tag, "ex " + t.getMessage());
            }
        });

Synchronous call interface 3

Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl("http://192.168.86.11:8087/").build();
        IServiceApi api = retrofit.create(IServiceApi.class);
        Call<LoginResult> call = api.login("test", "test1234");
        try {
            Response<LoginResult> resultResponse = call.execute();
            LoginResult result = resultResponse.body();
        }catch (Exception e){
            e.printStackTrace();
        }

Source code interpretation:

A: asynchronous call

1. Create a Retrofit object.

2. Reprofit. Create (iserviceapi. Class); / / use Proxy.newProxyInstance to create an interface proxy object. Internally use ServiceMethod with OkHttpCall to construct the object data they need

3. Call enqueue, internally construct the okhttp3.Call object, and execute the enqueue method of the object. Then in the okhttp3$Callback method, the callback of retrofit2 is called, and the assignment succeeds and fails.

B: synchronous call

1. In the same way, synchronous call is also called. The execute method of okhtt3.Call.

Source code: https://github.com/square/retrofit

Introduction:

implementation 'com.squareup.retrofit2:retrofit:2.0.2'
implementation 'com.squareup.retrofit2:converter-gson:2.0.2'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.retrofit2:adapter-rxjava:2.0.2'

Tags: Android Retrofit OkHttp github Google

Posted on Mon, 27 Apr 2020 07:37:10 -0700 by Miko