Model层下接口以及工具类

本文介绍了一个基于Retrofit实现网络请求的例子,并结合MVP模式进行架构分层设计,展示了如何通过Retrofit进行网络数据获取,以及如何在不同层间进行数据传递。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

工具类

public class MyApplication extends Application {

    public static GetDataInterface getDataInterface;

    @Override
    public void onCreate() {
        super.onCreate();
        //1.  用于全局配置初始化Fresco 图片加载
        Fresco.initialize(this);
        //2.  用于全局配置初始化Retrofit 网络请求
        //构建Retrofit类,初始化参数
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://result.eolinker.com")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        //3.//创建网络请求接口实例
        getDataInterface = retrofit.create(GetDataInterface.class);
    }
}

MyModel 层

public class MyModel {


    public void getDataM(final ModelCallBack modelCallBack){
        Call<DataDataBean> into = MyApplication.getDataInterface.getInto();
        //发起异步请求
        into.enqueue(new Callback<DataDataBean>() {
            @Override
            public void onResponse(Call<DataDataBean> call, Response<DataDataBean> response) {
                //获取响应的数据
                if (response.body()!=null&&response.isSuccessful()){
                    DataDataBean dataDataBean = response.body();
                    modelCallBack.onSuccess(dataDataBean);
                }
            }

            @Override
            public void onFailure(Call<DataDataBean> call, Throwable t) {
                //请求失败时返回数据
                modelCallBack.onFailure(new Exception(""));
            }
        });
    }
}

DateDataBean  数据接口


Model层下接口

public interface ModelCallBack {
    public void onSuccess(DataDataBean dataDataBean);
    public void onFailure(Exception  e);
}

Present层下 


import xieshuaikang.com.www.zhoukao1_moni_20171230.Model.DataDataBean;
import xieshuaikang.com.www.zhoukao1_moni_20171230.Model.Interfac.ModelCallBack;
import xieshuaikang.com.www.zhoukao1_moni_20171230.Model.MyModel;
import xieshuaikang.com.www.zhoukao1_moni_20171230.View.Fragment.ShouYe_Fragment;
import xieshuaikang.com.www.zhoukao1_moni_20171230.View.Interfa.MyView;

/**
 * Created by Administrator on 2017/12/30.
 */

public class MyPresenter {

    private  MyModel myModel;
    MyView myView;

    public MyPresenter(MyView myView) {
        myModel = new MyModel();
        this.myView = myView;
    }


    public void getData() {

        myModel.getDataM(new ModelCallBack() {
            @Override
            public void onSuccess(DataDataBean dataDataBean) {
                //数据交互时,为防止内存泄露,设置view层数据为空
                if (myView!=null){
                    myView.onSuccess(dataDataBean);
                }
            }

            @Override
            public void onFailure(Exception e) {
                //数据交互时,为防止内存泄露,设置view层数据为空
                if (myView != null){
                    myView.onFailure(e);
                }
            }

        });
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值