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/”;
}