本文整理汇总了Java中okhttp3.OkHttpClient.Builder.build方法的典型用法代码示例。如果您正苦于以下问题:Java Builder.build方法的具体用法?Java Builder.build怎么用?Java Builder.build使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类okhttp3.OkHttpClient.Builder
的用法示例。
在下文中一共展示了Builder.build方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: usingOkHttp3
import okhttp3.OkHttpClient.Builder; //导入方法依赖的package包/类
static ClientHttpRequestFactory usingOkHttp3(ClientOptions options)
throws IOException, GeneralSecurityException {
SSLSocketFactory socketFactory = SSLContext.getDefault().getSocketFactory();
X509TrustManager trustManager = getTrustManager();
Builder builder = new Builder().sslSocketFactory(socketFactory, trustManager);
if (options.getConnectionTimeout() != null) {
builder.connectTimeout(options.getConnectionTimeout(), TimeUnit.MILLISECONDS);
}
if (options.getReadTimeout() != null) {
builder.readTimeout(options.getReadTimeout(), TimeUnit.MILLISECONDS);
}
return new OkHttp3ClientHttpRequestFactory(builder.build());
}
示例2: usingOkHttp3
import okhttp3.OkHttpClient.Builder; //导入方法依赖的package包/类
static ClientHttpRequestFactory usingOkHttp3(ClientOptions options,
SslConfiguration sslConfiguration) throws GeneralSecurityException,
IOException {
Builder builder = new Builder();
if (hasSslConfiguration(sslConfiguration)) {
builder.sslSocketFactory(getSSLContext(sslConfiguration)
.getSocketFactory());
}
builder.connectTimeout(options.getConnectionTimeout().toMillis(),
TimeUnit.MILLISECONDS).readTimeout(
options.getReadTimeout().toMillis(), TimeUnit.MILLISECONDS);
return new OkHttp3ClientHttpRequestFactory(builder.build());
}
示例3: initImpl
import okhttp3.OkHttpClient.Builder; //导入方法依赖的package包/类
private void initImpl() {
synchronized (this) {
if (impl == null) {
Builder builder = new OkHttpClient.Builder();
if (networkTimeout != null) {
builder.readTimeout(networkTimeout, TimeUnit.MILLISECONDS);
}
for (OkHttpAdapterListener listener : listeners) {
listener.onBuild(builder);
}
impl = builder.build();
}
}
}
示例4: providesOkHttpClient
import okhttp3.OkHttpClient.Builder; //导入方法依赖的package包/类
@Provides
@Singleton
OkHttpClient providesOkHttpClient(Cache cache) {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
Builder builder = new Builder()
.connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)
.readTimeout(TIMEOUT, TimeUnit.SECONDS)
.addInterceptor(loggingInterceptor)
.addInterceptor(new AuthorizationInterceptor())
.cache(cache);
return builder.build();
}
示例5: RawRequest
import okhttp3.OkHttpClient.Builder; //导入方法依赖的package包/类
public RawRequest(@NonNull final String ampacheUrl,@NonNull String ampacheUser,@NonNull String ampachePassword){
mAmpachePassword = ampachePassword;
mAmpacheUrl = ampacheUrl;
mAmpacheUser = ampacheUser;
Builder bLong = new Builder()
.readTimeout(99, TimeUnit.SECONDS)
.writeTimeout(99, TimeUnit.SECONDS)
.connectTimeout(99, TimeUnit.SECONDS);
addUntrusted(bLong, ampacheUrl);
clientLongTimeout = bLong.build();
Builder bMedium = new Builder()
.readTimeout(44, TimeUnit.SECONDS)
.writeTimeout(44, TimeUnit.SECONDS)
.connectTimeout(44, TimeUnit.SECONDS);
addUntrusted(bMedium, ampacheUrl);
clientMediumTimeout = bMedium.build();
Builder bShort = new Builder()
.readTimeout(11, TimeUnit.SECONDS)
.writeTimeout(11, TimeUnit.SECONDS)
.connectTimeout(11, TimeUnit.SECONDS);
addUntrusted(bShort, ampacheUrl);
clientShortTimeout = bShort.build();
}
示例6: initImpl
import okhttp3.OkHttpClient.Builder; //导入方法依赖的package包/类
private synchronized void initImpl() {
if (impl == null) {
Builder builder = new OkHttpClient.Builder();
if (networkTimeout != null) {
builder.readTimeout(networkTimeout, TimeUnit.MILLISECONDS);
}
for (OkHttpAdapterListener listener : listeners) {
listener.onBuild(builder);
}
impl = builder.build();
}
}
示例7: provideOkHttpClient
import okhttp3.OkHttpClient.Builder; //导入方法依赖的package包/类
private OkHttpClient provideOkHttpClient(Context context, Interceptor interceptor) {
Builder builder = new Builder()
.connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)
.readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
.addInterceptor(interceptor)
.cache(new Cache(context.getCacheDir(), CACHE_SIZE));
return builder.build();
}
示例8: trustAllSslClient
import okhttp3.OkHttpClient.Builder; //导入方法依赖的package包/类
public static OkHttpClient trustAllSslClient(OkHttpClient client) {
log.warn("Using the trustAllSslClient is highly discouraged and should not be used in Production!");
Builder builder = client.newBuilder();
builder.sslSocketFactory(trustAllSslSocketFactory, (X509TrustManager)trustAllCerts[0]);
builder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
return builder.build();
}
示例9: createOkHttpClient
import okhttp3.OkHttpClient.Builder; //导入方法依赖的package包/类
private static OkHttpClient createOkHttpClient(List<Interceptor> appInterceptors,
List<Interceptor> networkInterceptors,
FreeSoundApiInterceptor freeSoundApiInterceptor) {
Builder okBuilder = new Builder();
okBuilder.interceptors().addAll(appInterceptors);
okBuilder.networkInterceptors().addAll(networkInterceptors);
okBuilder.interceptors().add(freeSoundApiInterceptor);
return okBuilder.build();
}
示例10: getOkHttpClient
import okhttp3.OkHttpClient.Builder; //导入方法依赖的package包/类
private static OkHttpClient getOkHttpClient() {
if (client == null) {
Builder b = new Builder();
b.connectTimeout(TIMEOUT_CONNECTION, TimeUnit.MILLISECONDS);
b.readTimeout(TIMEOUT_SOCKET, TimeUnit.MILLISECONDS);
client = b.build();
/*
client.setConnectTimeout(TIMEOUT_CONNECTION, TimeUnit.MILLISECONDS);
client.setReadTimeout(TIMEOUT_SOCKET, TimeUnit.MILLISECONDS);
*/
}
return client;
}
示例11: postHttp
import okhttp3.OkHttpClient.Builder; //导入方法依赖的package包/类
private String postHttp(String url, String postData) {
String result = "";
Builder builder = new Builder();
builder.connectTimeout(10000, TimeUnit.MILLISECONDS);
builder.readTimeout(10000,TimeUnit.MILLISECONDS);
builder.writeTimeout(10000,TimeUnit.MILLISECONDS);
builder.followRedirects(true);
builder.retryOnConnectionFailure(false);
OkHttpClient client = builder.build();
//MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");
RequestBody body = new FormBody.Builder().add("contents", postData).build();
//RequestBody.create(mediaType, "contents=" + postData);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
try {
Response response = client.newCall(request).execute();
result = response.body().string();
} catch (Exception e) {
e.printStackTrace();
}
return result;
/*
String result = "";
if (postData.trim().length() == 0) {
return "";
}
HttpPost httppost = new HttpPost(url);
CloseableHttpClient httpClient = HttpClients.createDefault();
//header
httppost.addHeader("connection", "close");
httppost.addHeader("user-agent", "Ragnar Fiery LogPusher");
httppost.addHeader("Content-Type", "application/x-www-form-urlencoded");
//Configure
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(10000)
.setConnectTimeout(10000)
.setConnectionRequestTimeout(20000)
.setContentCompressionEnabled(true)
.setExpectContinueEnabled(true)
.setMaxRedirects(3)
.setRedirectsEnabled(true)
.build();
httppost.setConfig(requestConfig);
//set parameter
List<NameValuePair> formparams = new ArrayList<>();
formparams.add(new BasicNameValuePair("contents", postData));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
httppost.setEntity(entity);
try {
CloseableHttpResponse response = httpClient.execute(httppost);
result = EntityUtils.toString(response.getEntity());
} catch (Exception e) {
e.printStackTrace();
log.error("http post error:" + e.getMessage());
}
httppost.releaseConnection();
return result;
*/
}
示例12: getCustomOkHttpClient
import okhttp3.OkHttpClient.Builder; //导入方法依赖的package包/类
private OkHttpClient getCustomOkHttpClient() {
final ConnectionSpec specModernTLS = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.cipherSuites(new CustomCipherSuites().getCustomCipherSuites().toArray(new CipherSuite[0]))
.build();
final ConnectionSpec specClearText = new ConnectionSpec.Builder(ConnectionSpec.CLEARTEXT)
.build();
final ConnectionSpec specCompatibleTLS = new ConnectionSpec.Builder(ConnectionSpec.COMPATIBLE_TLS)
.build();
final List<ConnectionSpec> specs = new ArrayList<>();
specs.add(specModernTLS);
specs.add(specClearText);
specs.add(specCompatibleTLS);
X509TrustManager trustManager;
SSLSocketFactory sslSocketFactory;
try {
trustManager = defaultTrustManager();
sslSocketFactory = defaultSslSocketFactory(trustManager);
SSLSocketFactory customSslSocketFactory =
new DelegatingSSLSocketFactory(sslSocketFactory) {
@Override
protected SSLSocket configureSocket(SSLSocket socket) throws IOException {
socket.setEnabledCipherSuites(javaNames(specModernTLS.cipherSuites()));
return socket;
}
};
Builder clientBuilder = new OkHttpClient.Builder().connectionSpecs(specs)
.sslSocketFactory(customSslSocketFactory, trustManager)
.connectTimeout(connectTimeoutTime, TimeUnit.MILLISECONDS)
.readTimeout(readTimeoutTime, TimeUnit.MILLISECONDS);
if (cookieJar != null) {
clientBuilder.cookieJar(cookieJar);
}
return clientBuilder.build();
} catch (GeneralSecurityException gse) {
}
return new OkHttpClient();
}