implementation 'io.reactivex:rxandroid:1.0.1'
implementation 'com.squareup.retrofit2:retrofit:2.0.2'
implementation 'com.squareup.retrofit2:converter-gson:2.0.2'
implementation 'com.squareup.retrofit2:adapter-rxjava:2.0.2'//
//日志拦截器
implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0'
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* date:2018/12/20
* author:QMY(QMY)
* function:
*/
public class RetrofitUtils {
private static RetrofitUtils mRetrofitUtils;
private final Retrofit mRetrofit;
//单例模式
public static RetrofitUtils getInstances(){
if (mRetrofitUtils==null){
synchronized (RetrofitUtils.class){
if (mRetrofitUtils==null){
return mRetrofitUtils=new RetrofitUtils();
}
}
}
return mRetrofitUtils;
}
private RetrofitUtils() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Log.e("message",message);
}
});
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
mRetrofit = new Retrofit.Builder()
.baseUrl(Api.URL_SHOPCART)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(getOkhttp())
.addInterceptor(interceptor)
.build();
}
private OkHttpClient getOkhttp(){
OkHttpClient httpClient = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.build();
return httpClient;
}
public Retrofit getRetrofit(){
return mRetrofit;
}
public <T> T create(Class<T> clazz){
return mRetrofit.create(clazz);
}
}
public class Api {
public final static String URL_SHOPCART="http://www.zhaoapi.cn/product/";
}
public interface Myservice {
@GET("getCarts?")
Observable<ShopCartBean> getShopCart(@Query("uid") String uid);
}
m层
public interface ShopModel {
Observable<ShopCartBean> getShop(String uid);
}
public class ShopModelImpl implements ShopModel{
@Override
public Observable<ShopCartBean> getShop(String uid) {
Myservice myservice = RetrofitUtils.getInstances().create(Myservice.class);
Observable<ShopCartBean> observable = myservice.getShopCart(uid);
return observable;
}
}
p层
public interface ShopCartPresenter {
void getShop(String uid);
}
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
/**
* date:2018/12/20
* author:QMY(QMY)
* function:
*/
public class ShopCartPresenterImpl implements ShopCartPresenter{
private final ShopCartView shopView;
private final ShopModelImpl mShopModel;
public ShopCartPresenterImpl(ShopCartView shopCartView) {
this.shopView=shopCartView;
mShopModel = new ShopModelImpl();
}
@Override
public void getShop(String uid) {
mShopModel.getShop(uid)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<ShopCartBean>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(ShopCartBean shopCartBean) {
shopView.getShop(shopCartBean.getData());
}
});
}
}
v层
public interface ShopCartView {
void getShop(List<ShopCartBean.DataBean> dataBeans);
}
再需要用到的地方调用p层
ShopCartPresenterImpl presenter = new ShopCartPresenterImpl(this);
presenter.getShop("71");