首先是一个P层的的逻辑代码
public class ShopPresenterextends BasePresenter{
private final Apiapi;
private final RetrofitHttpintence;
public ShopPresenter(){
intence = RetrofitHttp.getIntence();
api =intence.api;
}
public void ShopData(){
Observable shopData =api.ShopData();
shopData.subscribeOn(Schedulers.io())
.subscribe(new Consumer() {
@Override
public void accept(ShopBean shopBean)throws Exception {
getView().onSuccess(shopBean);
}
});
}
}
接下来就是网络请求的代码
这个代码我们可以服用在平时切换的网络的时候只有更改一下
//.baseUrl("http://172.17.8.100/")就可以了;
public class RetrofitHttp {
private static volatile RetrofitHttpretrofitHttp;
public final Apiapi;
public RetrofitHttp(){
OkHttpClient okHttpClient =new OkHttpClient.Builder().build();
Retrofit build =new Retrofit.Builder()
.baseUrl("http://172.17.8.100/")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
api = build.create(Api.class);
}
public static RetrofitHttp getIntence(){
if(retrofitHttp ==null){
synchronized (RetrofitHttp.class){
if(retrofitHttp ==null){
return new RetrofitHttp();
}
}
}
return retrofitHttp;
}
}