-
-
Notifications
You must be signed in to change notification settings - Fork 458
初始化
liujingxing edited this page Dec 28, 2019
·
1 revision
初始化是非必须的,如下:
//设置debug模式,默认为false,设置为true后,发请求,过滤"RxHttp"能看到请求日志
RxHttp.setDebug(boolean debug)
//非必须,只能初始化一次,第二次将抛出异常
RxHttp.init(OkHttpClient okHttpClient)
//或者,调试模式下会有日志输出
RxHttp.init(OkHttpClient okHttpClient, boolean debug)
如需要添加拦截器等其他业务需求,则可调用init
方法进行初始化,不初始化或者传入null
即代表使用默认OkHttpClient对象,建议在Application中初始化,默认的OkHttpClient对象在HttpSender类中可以找到,如下:
private static OkHttpClient getDefaultOkHttpClient() {
X509TrustManager trustAllCert = new X509TrustManagerImpl();
SSLSocketFactory sslSocketFactory = new SSLSocketFactoryImpl(trustAllCert);
return new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.sslSocketFactory(sslSocketFactory, trustAllCert) //添加信任证书
.hostnameVerifier((hostname, session) -> true) //忽略host验证
.build();
}
虽然初始化是非必须的,但是建议大家传入自定义的OkHttpClient对象,一来,自定义的OkHttpClient能最大化满足自身的业务;二来,随着RxHttp版本的升级,默认的OkHttpClient可能会发生变化(虽然可能性很小),故建议自定义OkHttpClient对象传入RxHttp。