首先你得有 OkHttp网络请求封装类 和 拦截器类
创建一个接口 CallBackView
public interface CallBackView {
void success(String json);
void failed(Exception e);
}
.Model
public class LoginModel {
//获取数据
public void getLoginData(String uri, Map<String,String> map,CallBackView callBackView) {
OkHttpUtil.getInstance().doPost(uri, map, callBackView);
}
}
.Presenter
public class LoginPresenter {
private CallBackView mCallBackView;
LoginModel mLoginModel = new LoginModel();
public void dettach(){
if (mCallBackView != null){
mCallBackView = null;
}
}
public void getLoginData(String uri, Map<String,String> map , CallBackView callBackView) {
mLoginModel.getLoginData(uri,map, callBackView);
}
}
调用P层,得到方法,解析数据
mHomePresenter = new HomePresenter();
mBannerAdapter = new BannerAdapter(getActivity());
homefragment_ViewPager_viewpager.setAdapter(mBannerAdapter);
mHomePresenter.getBannerData(uri_banner, new CallBackView() {
@Override
public void success(String json) {
mBannerBean = new Gson().fromJson(json, BannerBean.class);
//更新数据
mBannerAdapter.setData(mBannerBean.getResult());
//自动轮播
handler.sendEmptyMessageDelayed(0, 2000);
}
@Override
public void failed(Exception e) {
}
});
最后解决内存泄漏
//内存泄漏
@Override
protected void onDestroy() {
super.onDestroy();
if (mLoginPresenter != null) {
mLoginPresenter.dettach();
}
}