use of com.gradle.android.retrofit.OkhttpUtils.TrustAllCerts in project Gradle-demo by Arisono.
the class RetrofitImpl method initClient.
@Override
public void initClient() {
// 本类保证初始化一次,减少系统开销
Builder okBuilder = new OkHttpClient.Builder().connectTimeout(mbuilder.getConnectTimeout(), TimeUnit.SECONDS).readTimeout(mbuilder.getReadTimeout(), TimeUnit.SECONDS).writeTimeout(mbuilder.getWriteTimeout(), TimeUnit.SECONDS).sslSocketFactory(OkhttpUtils.createSSLSocketFactory(), // 信任所有证书
new TrustAllCerts()).hostnameVerifier(new TrustAllHostnameVerifier());
LogInterceptor logInterceptor = new LogInterceptor();
logInterceptor.setBuilder(mbuilder);
okBuilder.addInterceptor(logInterceptor);
if (mbuilder.getCacheFileSize() != 0) {
okBuilder.cache(new Cache(mbuilder.getCacheFile(), mbuilder.getCacheFileSize()));
okBuilder.addInterceptor(new CacheInterceptor(String.valueOf(mbuilder.getCacheTime()), mbuilder.getCacheType()));
}
//后期缓存策略改进
switch(mbuilder.getCacheType()) {
case CacheType.ONLY_NETWORK:
OkhttpUtils.println("CacheType.ONLY_NETWORK");
break;
case CacheType.ONLY_CACHED:
OkhttpUtils.println("CacheType.ONLY_CACHED");
break;
case CacheType.CACHED_ELSE_NETWORK:
break;
case CacheType.NETWORK_ELSE_CACHED:
break;
default:
break;
}
OkHttpClient client = okBuilder.build();
retrofit = new Retrofit.Builder().client(client).baseUrl(mbuilder.getBaseUrl()).addConverterFactory(StringConverterFactory.create()).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();
}
use of com.gradle.android.retrofit.OkhttpUtils.TrustAllCerts in project Gradle-demo by Arisono.
the class OkhttpUtils method initClient.
public static void initClient(HttpClient mbuilder) {
//本类保证初始化一次,减少系统开销
okhttp3.OkHttpClient.Builder okBuilder = client.newBuilder().connectTimeout(mbuilder.getConnectTimeout(), TimeUnit.SECONDS).readTimeout(mbuilder.getReadTimeout(), TimeUnit.SECONDS).writeTimeout(mbuilder.getWriteTimeout(), TimeUnit.SECONDS).sslSocketFactory(OkhttpUtils.createSSLSocketFactory(), //信任所有证书
new TrustAllCerts()).hostnameVerifier(new TrustAllHostnameVerifier());
LogInterceptor logInterceptor = new LogInterceptor();
logInterceptor.setBuilder(mbuilder);
okBuilder.addInterceptor(logInterceptor);
client = okBuilder.build();
}
use of com.gradle.android.retrofit.OkhttpUtils.TrustAllCerts in project Gradle-demo by Arisono.
the class OkhttpUtils method createSSLSocketFactory.
public static SSLSocketFactory createSSLSocketFactory() {
SSLSocketFactory ssfFactory = null;
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, new TrustManager[] { new TrustAllCerts() }, new SecureRandom());
ssfFactory = sc.getSocketFactory();
} catch (Exception e) {
}
return ssfFactory;
}
Aggregations