Retrofit和OkHttp的简单配合使用

在我们的请求网络中,有多种网络请求方式,那么Retrofit和OkHttp两种网络请求方式也是能一起配合使用的,Retrofit和OkHttp都是由Square公司发布的,Retrofit的底层封装的是OkHttp,我在这用的是MVP模式,封装一个网络请求~~

自定义的工具类

public class RetrofitUtils {
    //单例模式
    private static volatile RetrofitUtils instance;
    private final Call<AllBean> call;
    //通过穿的参数形式进行网络请求
    private RetrofitUtils(Map<Object,Object> map,String uri){
        //创建OkHttp网络请求对象
        OkHttpClient build1 = new OkHttpClient.Builder().build();
        //创建一个Retrofit的创建对象
        Retrofit build = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                //通过这个方法将Okhttp放进Retrofit中,实现结合使用
                .client(build1)
                .baseUrl(uri)
                .build();
        //APIInterface是我们自定义的接口类,用来拼接地址
        APIInterface apiInterface = build.create(APIInterface.class);
        //调用接口方法,返回一个Call对象
        call = apiInterface.call(map);
    };
    //单例模式的判断
    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;
    }
    //定义一个方法,用来让调用这个类的对象获得Call对象
    public Call<AllBean> getBuild(){
        return call;
    }
}

自定义的接口类,用来拼接地址进行网络请求

public interface APIInterface {
//定义的请求方式
    @GET("你要放置的地址")
    //泛型中放的是返回的Bean类,后面的参数放的是map集合,存放拼接的参数
    Call<AllBean> call(@QueryMap Map<Object,Object> map);
}

M层的代码

//M层实现自己的接口
public class Model implements ModelInterface{
    //实例化P层
    private Persent persent;
    public Model(Persent persent){
        this.persent = persent;
    }
    @Override
    public void getData(String uri, Map<Object, Object> map) {
        //通过工具类将创建好的Retrofit对象返回的Bean类取出来
        Call<AllBean> build = RetrofitUtils.getInstance(map, uri).getBuild();
        //调用P层的get方法,用来将请求的Call对象传过去
        persent.get(build);
    }
}

M层的接口类:

public interface ModelInterface {
    //用来传递地址和要拼接的参数
    void getData(String uri, Map<Object,Object> map);
}

P层的代码块:

//P层实现本层的接口,用来传递要访问的地址和拼接的参数
public class Persent implements PersentInterface{
    //声明V层接口,将成功失败的结果返回
    private ViewInterface viewInterface;
    public Persent(ViewInterface viewInterface){
        this.viewInterface = viewInterface;
    }
    @Override
    public void getData(String uri, Map<Object, Object> map) {
        //将P层对象传到M层,用多态的方式取到M层的接口方法
        ModelInterface model = new Model(this);
        //将地址和参数通过传参的形式传递
        model.getData(uri,map);
    }
    //定义一个方法,用来接收M层传递的Call对象
    public void get(Call<AllBean> bean){
        //通过Call对象异步请求数据
        bean.enqueue(new Callback<AllBean>() {
            //请求成功
            @Override
            public void onResponse(Call<AllBean> call, Response<AllBean> response) {
                viewInterface.Success(response.body());
            }
            //请求失败
            @Override
            public void onFailure(Call<AllBean> call, Throwable t) {
                viewInterface.Failed(t.getMessage());
            }
        });
    }
}

P层的接口代码:

public interface PersentInterface {
    //定义一个相同的方法用来传递参数
    void getData(String uri, Map<Object,Object> map);
}

V层在使用网络请求的时候,只需要实例化P层,然后实现自己的接口,V层的接口方法里定义的也是一个成功和失败的方法,V层也只需要实现一下自己的接口,就可以获得成功和失败的数据~~

猜你喜欢

转载自blog.csdn.net/dealpoor/article/details/78785660