Simple combination of Retrofit and OkHttp

Posted by jbulaswad on Sun, 31 May 2020 19:11:27 +0200

In our request network, there are many kinds of network request modes, so two kinds of network request modes, Retrofit and OkHttp, can be used together. Both Retrofit and OkHttp are released by Square company. The bottom layer of Retrofit encapsulates OkHttp. I use MVP mode here to encapsulate a network request~~

Custom tool class

public class RetrofitUtils {
    //Singleton mode
    private static volatile RetrofitUtils instance;
    private final Call<AllBean> call;
    //Network request in the form of wearing parameters
    private RetrofitUtils(Map<Object,Object> map,String uri){
        //Create OkHttp network request object
        OkHttpClient build1 = new OkHttpClient.Builder().build();
        //Create a Retrofit creation object
        Retrofit build = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                //In this way, Okhttp is put into Retrofit, which can be used in combination
                .client(build1)
                .baseUrl(uri)
                .build();
        //APIInterface is our custom interface class, which is used to splice addresses
        APIInterface apiInterface = build.create(APIInterface.class);
        //Call interface method, return a call object
        call = apiInterface.call(map);
    };
    //Judgment of singleton mode
    public static RetrofitUtils getInstance(Map<Object,Object> map,String uri){
        if(instance == null){
            synchronized(RetrofitUtils.class){
                if(instance == null){
                    instance = new RetrofitUtils(map,uri);
                }
            }
        }
        return instance;
    }
    //Define a method to get the Call object from the object calling this class
    public Call<AllBean> getBuild(){
        return call;
    }
}

User defined interface class, used to splice addresses for network requests

public interface APIInterface {
//Request method defined
    @GET("The address you want to place")
    //The returned Bean class is put in the generics, and the map set is put in the later parameters to store the spliced parameters
    Call<AllBean> call(@QueryMap Map<Object,Object> map);
}

Code of layer M

//M layer implements its own interface
public class Model implements ModelInterface{
    //Instantiate P layer
    private Persent persent;
    public Model(Persent persent){
        this.persent = persent;
    }
    @Override
    public void getData(String uri, Map<Object, Object> map) {
        //Take out the Bean class returned by the created Retrofit object through the tool class
        Call<AllBean> build = RetrofitUtils.getInstance(map, uri).getBuild();
        //Call the get method of the P layer to pass the requested call object
        persent.get(build);
    }
}

Interface class of layer M:

public interface ModelInterface {
    //Used to pass address and parameters to be spliced
    void getData(String uri, Map<Object,Object> map);
}

Code block of P layer:

//The P layer implements the interface of this layer, which is used to pass the address to be accessed and the splicing parameters
public class Persent implements PersentInterface{
    //Declare the V-layer interface and return the successful and failed results
    private ViewInterface viewInterface;
    public Persent(ViewInterface viewInterface){
        this.viewInterface = viewInterface;
    }
    @Override
    public void getData(String uri, Map<Object, Object> map) {
        //Transfer the objects of P layer to M layer, and get the interface method of M layer by means of polymorphism
        ModelInterface model = new Model(this);
        //Pass the address and parameters in the form of parameters
        model.getData(uri,map);
    }
    //Define a method to receive the Call object passed by layer M
    public void get(Call<AllBean> bean){
        //Request data asynchronously through Call object
        bean.enqueue(new Callback<AllBean>() {
            //Request succeeded
            @Override
            public void onResponse(Call<AllBean> call, Response<AllBean> response) {
                viewInterface.Success(response.body());
            }
            //request was aborted
            @Override
            public void onFailure(Call<AllBean> call, Throwable t) {
                viewInterface.Failed(t.getMessage());
            }
        });
    }
}

Interface code of layer P:

public interface PersentInterface {
    //Define the same method to pass parameters
    void getData(String uri, Map<Object,Object> map);
}

When v layer uses network request, it only needs to instantiate P layer, and then realize its own interface. V layer's interface method also defines a success and failure method. V layer only needs to implement its own interface to obtain success and failure data~~

Topics: network Retrofit OkHttp