okhttp+rxjava2+retrofit2封装类

   public static final int DEFAULT_TIMEOUT = 5;
        public static final String URL="https://www.zhaoapi.cn/";
        private Retrofit mRetrofit;
        private ApiService mApiService;

        private static RetrofitUtil mInstance;

        /**
         * 私有构造方法
         */
        private RetrofitUtil(){
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            OkHttpClient.Builder builder = new OkHttpClient.Builder();
            builder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
            builder.addInterceptor(interceptor).addInterceptor(new MyInterceptor());//网络拦

            mRetrofit = new Retrofit.Builder()
                    .client(builder.build())
                    .baseUrl(URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();
            mApiService = mRetrofit.create(ApiService.class);
        }

        public static RetrofitUtil getInstance(){
            if (mInstance == null){
                synchronized (RetrofitUtil.class){
                    if (null == mInstance) {
                        mInstance = new RetrofitUtil();
                    }
                }
            }
            return mInstance;
        }

        public ApiService getUsers(){
            return mApiService;
        }
private class MyInterceptor implements Interceptor {
    @Override
    public okhttp3.Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        HttpUrl httpUrl = request
                .url()
                .newBuilder()
                .addQueryParameter("source", "android")
                .build();
        Request requestNew = request
                .newBuilder()
                .url(httpUrl)
                .build();
        return chain.proceed(requestNew);
    }
}
model层引用:

 
黄油刀和rxjava2+retrofit2依赖
implementation 'com.jakewharton:butterknife:8.8.1’
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
implementation 'io.reactivex.rxjava2:rxjava:2.1.12'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
implementation 'com.afollestad.material-dialogs:core:0.9.6.0'
implementation 'com.afollestad.material-dialogs:commons:0.9.6.0'
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'com.google.code.gson:gson:2.8.2'
 RetrofitUtil.getInstance().getUsers()
            .getProductCatagory(cid)
            .subscribeOn(Schedulers.io())
            .unsubscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new DisposableSubscriber<ProductCatagoryBean>() {
                @Override
                public void onNext(ProductCatagoryBean productCatagoryBeanBaseBean) {
                    Log.e(TAG, "onNext: "+productCatagoryBeanBaseBean.getMsg() );
                    toHome.toBack(productCatagoryBeanBaseBean);
                }

                @Override
                public void onError(Throwable t) {
                    Log.e(TAG, "onError: "+t.getMessage() );
                }

                @Override
                public void onComplete() {

                }
            });
}
retrofit接口引用:

@GET("product/getProductCatagory")
Flowable<ProductCatagoryBean> getProductCatagory(@Query("cid") String cid);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值