用的

BaseView层所有都继承

package com.example.myapplication.http.Base;

public interface IBaseView {

}

继承的View层 可以重复的调用

public interface IShowView extends IBaseView {
void onSuccess(T t);
void onFaileEd(String err);
}

BasePresenter公共的可以是一个的

public class BasePresenter {
private T view;

public T getView() {
    return view;
}

public void setView(T view) {
    this.view = view;
}
public void onDeatch(){
    this.view=null;
}

}

写入的Presenter层次

public class ShowPresenter extends BasePresenter {
public void getData(int page,int count){
Apinterface apinterface = RetrofitUtils.getInstance().create(Apinterface.class);
apinterface.getshow(page,count).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer() {
@Override
public void accept(ShowBean showBean) throws Exception {
getView().onSuccess(showBean);
}
});
}
}

View的实现也就是我们的Activity或者是Fragment
public class Main2Activity extends AppCompatActivity implements IShowView {

private ShowPresenter showPresenter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    initView();
}

private void initView() {
    showPresenter = new ShowPresenter();
    showPresenter.setView(this);
    showPresenter.getData(1,5);
}

@Override
public void onSuccess(ShowBean showBean) {
    String message = showBean.getMessage();

}

@Override
public void onFaileEd(String err) {

}

@Override
protected void onDestroy() {
    super.onDestroy();
	//一定要解绑
    showPresenter.onDeatch();
}

}

网络工具类

public class RetrofitUtils {

private static RetrofitUtils instance;
private final Retrofit retrofit;

//单例模式
public static RetrofitUtils getInstance(){
    if (instance == null){
        synchronized (RetrofitUtils.class){
            if (instance == null){
                instance = new RetrofitUtils();
            }
        }
    }
    return instance;
}

//网络请求
private RetrofitUtils(){
    retrofit = new Retrofit.Builder()
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl(Api.BASE)
            .build();
}

//将单例模式传输到presenter层里使用
public <T> T create(final Class<T> service){
    return retrofit.create(service);
}

}

ApiService

public interface Apinterface {
@GET(“small/commodity/v1/commodityList”)
Observable getshow(@Query(“page”) int page,@Query(“count”) int count);
}

Api

public class Api {
//请求接口
public final static String BASE = “http://172.17.8.100/”;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值