retrofit+rxjava的解析工具类

public class RetrofitUtils {

    private static final long DEFAULT_TIMEOUT = 20000;


    private static volatile ApiService mApiService;

    public static ApiService getService() {

        if (mApiService == null) {
            synchronized (RetrofitUtils.class) {
                if (mApiService == null) {
                    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
                    if (BuildConfig.DEBUG) {
                        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
                    } else {
                        logging.setLevel(HttpLoggingInterceptor.Level.NONE);
                    }


                    OkHttpClient httpClient = new OkHttpClient.Builder()
                            .addInterceptor(logging)
                            .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                            .writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                            .readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                            .build();

                    Retrofit retrofit = new Retrofit.Builder()
                            .client(httpClient)
                            //          new Retrofit2ConverterFactory()        GsonConverterFactory.create()
                            .addConverterFactory(GsonConverterFactory.create())
                            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                            .baseUrl(ApiService.url)
                            .build();
                    mApiService = retrofit.create(ApiService.class);
                }
            }
        }
        return mApiService;
    }

}

解释:
ApiService --------你自己写的api接口
HttpLoggingInterceptor -----日志拦截器,需要导入依赖
工具类需要的依赖有:

//拦截器
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.'
//RXJava:
 implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0' // 必要依赖,和Rxjava结合必须用到,下面会提到
	implementation "io.reactivex.rxjava2:rxjava:2.1.3" // 必要rxjava2依赖
   	 implementation "io.reactivex.rxjava2:rxandroid:2.0.1" // 必要rxandrroid依赖,切线程时需要用到

//OkHttp网络解析
	implementation 'com.squareup.okhttp3:okhttp:3.2.0'
	implementation 'com.squareup.okio:okio:1.7.0'

//Retrofit网络解析
	implementation 'com.squareup.retrofit2:retrofit:2.3.0'
	implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值