BigDemo----MP

IVIew

public interface IView {
    Context context();
}

DataCall

public interface DataCall extends IView {
    //商品列表接口--传列表对象
    void ShowSuccess(GoodsBean goodsBean);

    //GridView展示--传对象
    void ShowGridSuccess(GrBean grBean);

    //购车车接口---传购物车集合
    void ShowShopSuccess(List<Shop.DataBean> dataBean);

    //未加载接口---打印错误信息
    void ShowError(String error);
}

HttpUtils

public class HttpUtils {
    public static HttpUtils httpUtils;
    private final Handler handler;
    private final OkHttpClient HttpClient;

    private HttpUtils() {
        //主线程Handler
        handler = new Handler(Looper.getMainLooper());
        HttpClient = new OkHttpClient.Builder()
                .readTimeout(5000, TimeUnit.MILLISECONDS)
                .writeTimeout(5000, TimeUnit.MILLISECONDS)
                .connectTimeout(5000, TimeUnit.MILLISECONDS)
                .connectionPool(new ConnectionPool(5, 1, TimeUnit.SECONDS))
                .build();
    }


    public static HttpUtils getHttpUtils() {
        if (httpUtils == null) {
            synchronized (HttpUtils.class) {
                if (httpUtils == null) {
                    return httpUtils = new HttpUtils();
                }
            }
        }
        return httpUtils;
    }

    //异步get
    public void doGet(String url, final IOKhttpUtilsCallback ioKhttpUtilsCallback) {
        Request request = new Request.Builder().get().url(url).build();
        Call call = HttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                if (ioKhttpUtilsCallback != null) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            ioKhttpUtilsCallback.onFailure(e.getMessage());
                        }
                    });
                }
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response != null && response.isSuccessful()) {
                    final String json = response.body().string();
                    if (ioKhttpUtilsCallback != null) {
                        //切换到主线程
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                ioKhttpUtilsCallback.onResponse(json);
                            }
                        });
                    }


                } else {
                    if (ioKhttpUtilsCallback != null) {
                        //切换到主线程
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                ioKhttpUtilsCallback.onFailure("网络异常");
                            }
                        });
                    }
                }
            }
        });

    }
    //异步post
    public void doPost(String url, Map<String, String> map, final IOKhttpUtilsCallback ioKhttpUtilsCallback) {
        FormBody.Builder builder = new FormBody.Builder();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            builder.add(entry.getKey(), entry.getValue());
        }
        FormBody formBody = builder.build();
        Request request = new Request.Builder()
                .post(formBody)
                .url(url)
                .build();
        Call call = HttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                if (ioKhttpUtilsCallback != null) {
                    //切换到主线程
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            ioKhttpUtilsCallback.onFailure(e.getMessage());
                        }
                    });
                }
            }


            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response != null && response.isSuccessful()) {
                    final String json = response.body().string();
                    if (ioKhttpUtilsCallback != null) {
                        //切换到主线程
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                ioKhttpUtilsCallback.onResponse(json);
                            }
                        });
                    }


                } else {
                    if (ioKhttpUtilsCallback != null) {
                        //切换到主线程
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                ioKhttpUtilsCallback.onFailure("网络异常");
                            }
                        });
                    }
                }
            }
        });
    }

    //接口回调
    public interface IOKhttpUtilsCallback {
        void onFailure(String error);

        void onResponse(String json);
    }
}

BasePresenter

public abstract class BasePresenter<V extends IView> {
    protected V view;

    public BasePresenter(V view) {
        this.view = view;
        initModel();
    }

    protected abstract void initModel();

    public void onDestroy() {
        view = null;
    }
}

GoodsPresenter

public class GoodsPresenter extends BasePresenter<DataCall> {


    private GoodsModel goodsModel;

    public GoodsPresenter(DataCall view) {
        super(view);

    }

    @Override
    protected void initModel() {
        //商品列表M层对象
        goodsModel = new GoodsModel();
    }

    //showGoods方法-传入page name
    public void showGoods(int page, String name) {
        //m对象调用showGoods
        goodsModel.showGoods(page, name, new GoodsModel.GoodsModelCallBack() {
            @Override
            public void getSuccess(GoodsBean goodsBean) {
                view.ShowSuccess(goodsBean);
            }

            @Override
            public void getFaid(String error) {
                view.ShowError(error);
            }
        });
    }
}

GridPresenter

public class GoodsPresenter extends BasePresenter<DataCall> {


    private GoodsModel goodsModel;

    public GoodsPresenter(DataCall view) {
        super(view);

    }

    @Override
    protected void initModel() {
        //商品列表M层对象
        goodsModel = new GoodsModel();
    }

    //showGoods方法-传入page name
    public void showGoods(int page, String name) {
        //m对象调用showGoods
        goodsModel.showGoods(page, name, new GoodsModel.GoodsModelCallBack() {
            @Override
            public void getSuccess(GoodsBean goodsBean) {
                view.ShowSuccess(goodsBean);
            }

            @Override
            public void getFaid(String error) {
                view.ShowError(error);
            }
        });
    }
}

ShopPresenter

public class ShopPresenter extends BasePresenter<DataCall> {

    private ShopModel shopModel;

    public ShopPresenter(DataCall view) {
        super(view);
    }

    @Override
    protected void initModel() {
        //购物车M层对象
        shopModel = new ShopModel();
    }

    public void showShop(String url) {
        shopModel.showShop(url, new ShopModel.ShopModelCallBack() {
            @Override
            public void getSuccess(List<Shop.DataBean> dataBean) {

                view.ShowShopSuccess(dataBean);
            }

            @Override
            public void getFaid(String error) {
                view.ShowError(error);
            }
        });
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值