okhttp3.OkHttpClient.Builder
public static final class OkHttpClient.Builder
OkHttpClient.Builder是用来帮助创建OkHttpClient的;通过它我们可以方便的对OkHttpClient进行设置;
下面是OkHttpClient.Builder设置OkHttpClient的常用方法:
public OkHttpClient.Builder connectTimeout(long timeout,TimeUnit unit)
public OkHttpClient.Builder readTimeout(long timeout,TimeUnit unit)
public OkHttpClient.Builder writeTimeout(long timeout,TimeUnit unit)
public OkHttpClient.Builder cookieJar(CookieJar cookieJar)
public OkHttpClient.Builder cache(@Nullable Cache cache)
public OkHttpClient.Builder addInterceptor(Interceptor interceptor)
public OkHttpClient.Builder addNetworkInterceptor(Interceptor interceptor)
public OkHttpClient.Builder retryOnConnectionFailure(boolean retryOnConnectionFailure)
public OkHttpClient.Builder proxy(@Nullable Proxy proxy)
public OkHttpClient.Builder proxySelector(ProxySelector proxySelector)
public OkHttpClient.Builder sslSocketFactory(SSLSocketFactory sslSocketFactory,X509TrustManager trustManager)
如果需要,您可以用以下代码自己创建和配置默认值:
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init((KeyStore) null);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
throw new IllegalStateException("Unexpected default trust managers:"
+ Arrays.toString(trustManagers));
}
X509TrustManager trustManager = (X509TrustManager) trustManagers[0];
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[] { trustManager }, null);
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient client = new OkHttpClient.Builder()
.sslSocketFactory(sslSocketFactory, trustManager)
.build();
public OkHttpClient.Builder dispatcher(Dispatcher dispatcher)
public OkHttpClient build()