Retrofit+okhttp+Rxjava 网络请求

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");
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值