实现一个非常简单的MVP框架

ApiService


public interface Api {
    // https://api.yunxuekeji.cn/yunxue_app_api/course/getCourseByTypeAndMore?
    // orderOn=&classtype=031001004&forPeopleType=&format=&price=&pageIndex=1&pageSize=11
    @FormUrlEncoded
    @POST("getCourseByTypeAndMore?")
    Observable<Bean> getMap(@FieldMap Map<String,String> map);
}

BaseView

public interface BaseView {
    public void shouView(Bean bean);
}

BaseModel

public interface BaseModel {
    public interface PostCallBack{
        public void showData(Bean bean);
    }

    public void getData(PostCallBack postCallBack, Map<String,String> map);
}

MainModel

public class MainModel implements BaseModel{
    // https://api.yunxuekeji.cn/yunxue_app_api/course/getCourseByTypeAndMore?
    // orderOn=&classtype=031001004&forPeopleType=&format=&price=&pageIndex=1&pageSize=11
    @Override
    public void getData(final PostCallBack postCallBack, Map<String, String> map) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.yunxuekeji.cn/yunxue_app_api/course/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();

        retrofit.create(Api.class).getMap(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Bean>() {
                    @Override
                    public void call(Bean bean) {
                        postCallBack.showData(bean);
                    }
                });
    }
}

BasePresenter

public interface BasePresenter {
    public void setMap(Map<String,String> map);
}

MainPresenter


public class MainPresenter implements BasePresenter,BaseModel.PostCallBack {
    public BaseView baseView;
    public BaseModel baseModel = new MainModel();
    public MainPresenter(BaseView baseView) {
        this.baseView = baseView;
    }
    @Override
    public void setMap(Map<String, String> map) {
          baseModel.getData(this,map);
    }

    @Override
    public void showData(Bean bean) {
        baseView.shouView(bean);
    }
}

MainActivity

public class MainActivity extends AppCompatActivity implements BaseView{
//https://api.yunxuekeji.cn/yunxue_app_api/course/getCourseByTypeAndMore?
// orderOn=&classtype=031001004&forPeopleType=&format=&price=&pageIndex=1&pageSize=11
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MainPresenter mainPresenter = new MainPresenter(this);
        Map<String,String> map =new HashMap<>();
        map.put("orderOn","");
        map.put("classtype","031001004");
        map.put("forPeopleType","");
        map.put("format","");
        map.put("price","");
        map.put("pageIndex","1");
        map.put("pageSize","11");
        mainPresenter.setMap(map);
    }

    @Override
    public void shouView(Bean bean) {
        List<Bean.BodyBean.ResultBean.DataBean> list = bean.getBody().getResult().getData();
        //Toast.makeText(MainActivity.this,list.size()+"",Toast.LENGTH_SHORT);
        Log.e("asdasdasd",list.size()+"");
    }
}

关于依赖

Retrofit+rXJava
 compile 'io.reactivex:rxjava:1.1.6'
    // Retrofit的rx解析库
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'

    // Retrofit的gson库字符串库
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.squareup.retrofit2:converter-scalars:2.1.0'
    compile 'com.orhanobut:logger:1.8'
    //解决Android3.0以后不能butterknife问题
    implementation 'com.jakewharton:butterknife:8.4.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
    compile 'io.reactivex:rxandroid:1.2.1'
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值