本文整理汇总了Java中okhttp3.OkHttpClient.Builder类的典型用法代码示例。如果您正苦于以下问题:Java Builder类的具体用法?Java Builder怎么用?Java Builder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Builder类属于okhttp3.OkHttpClient包,在下文中一共展示了Builder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getConnection
import okhttp3.OkHttpClient.Builder; //导入依赖的package包/类
public InfluxDB getConnection()
{
Assert.notNull(getProperties(), "InfluxDBProperties are required");
if (connection == null)
{
final Builder client = new OkHttpClient.Builder()
.connectTimeout(properties.getConnectTimeout(), TimeUnit.SECONDS)
.writeTimeout(properties.getWriteTimeout(), TimeUnit.SECONDS)
.readTimeout(properties.getReadTimeout(), TimeUnit.SECONDS);
connection = InfluxDBFactory
.connect(properties.getUrl(), properties.getUsername(), properties.getPassword(), client);
logger.debug("Using InfluxDB '{}' on '{}'", properties.getDatabase(), properties.getUrl());
if (properties.isGzip())
{
logger.debug("Enabled gzip compression for HTTP requests");
connection.enableGzip();
}
}
return connection;
}
示例2: getHeader
import okhttp3.OkHttpClient.Builder; //导入依赖的package包/类
public static Builder getHeader(String userName, String password) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(Level.BASIC);
OkHttpClient.Builder okBuilder = new OkHttpClient.Builder();
okBuilder.authenticator(new Authenticator() {
@Override
public Request authenticate(Route route, Response response) throws IOException {
String credential = Credentials.basic(userName, password);
if (credential.equals(response.request().header("Authorization"))) {
try {
FortifyExceptionUtil.verifyFortifyResponseCode(response.code(), "Unauthorized access of Fortify Api");
} catch (IntegrationException e) {
throw new IOException(e);
}
return null;
}
return response.request().newBuilder().header("Authorization", credential).build();
}
});
okBuilder.addInterceptor(logging);
return okBuilder;
}
示例3: usingHttpComponents
import okhttp3.OkHttpClient.Builder; //导入依赖的package包/类
static ClientHttpRequestFactory usingHttpComponents(ClientOptions options)
throws GeneralSecurityException, IOException {
HttpClientBuilder httpClientBuilder = HttpClients.custom()
.setSSLContext(SSLContext.getDefault())
.useSystemProperties();
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom()
.setAuthenticationEnabled(true);
if (options.getConnectionTimeout() != null) {
requestConfigBuilder.setConnectTimeout(options.getConnectionTimeout());
}
if (options.getReadTimeout() != null) {
requestConfigBuilder.setSocketTimeout(options.getReadTimeout());
}
httpClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
return new HttpComponentsClientHttpRequestFactory(httpClientBuilder.build());
}
示例4: 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());
}
示例5: 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());
}
示例6: 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();
}
}
}
示例7: 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();
}
示例8: 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();
}
示例9: setNetworkTimeout
import okhttp3.OkHttpClient.Builder; //导入依赖的package包/类
public static void setNetworkTimeout(CrnkClient client, final int timeout, final TimeUnit timeUnit) {
OkHttpAdapter httpAdapter = (OkHttpAdapter) client.getHttpAdapter();
httpAdapter.addListener(new OkHttpAdapterListenerBase() {
@Override
public void onBuild(Builder builder) {
builder.readTimeout(timeout, timeUnit);
}
});
}
示例10: 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();
}
}
示例11: test
import okhttp3.OkHttpClient.Builder; //导入依赖的package包/类
@Test
public void test() {
ResourceRepositoryV2<Task, Long> taskRepo = client.getRepositoryForType(Task.class);
Task task = new Task();
task.setId(1L);
task.setName("task");
taskRepo.create(task);
List<Task> tasks = taskRepo.findAll(new QuerySpec(Task.class));
Assert.assertEquals(1, tasks.size());
Mockito.verify(testModule, Mockito.times(1)).setupModule(Mockito.any(ModuleContext.class));
Mockito.verify(testModule, Mockito.times(1)).setHttpAdapter(Mockito.eq(client.getHttpAdapter()));
Mockito.verify(adapterListener, Mockito.times(1)).onBuild(Mockito.any(Builder.class));
}
示例12: buildOkkHttpInterceptor
import okhttp3.OkHttpClient.Builder; //导入依赖的package包/类
private Interceptor buildOkkHttpInterceptor(){
return (chain -> {
Request originalRequest = chain.request();
HttpUrl originalHttpUrl = originalRequest.url();
HttpUrl.Builder builder=originalHttpUrl.newBuilder()
.addEncodedQueryParameter(API_QUERY,clientId);
if(token!=null){
builder.addEncodedQueryParameter(OAUTH_TOKEN,token.access);
}
HttpUrl newHttpUrl = builder.build();
Request newRequest = originalRequest.newBuilder()
.url(newHttpUrl).build();
return chain.proceed(newRequest);});
}
示例13: 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();
}
示例14: provideRetrofit
import okhttp3.OkHttpClient.Builder; //导入依赖的package包/类
private Retrofit provideRetrofit(OkHttpClient okHttpClient) {
Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(new Adapter())
.create();
return new Retrofit.Builder()
.baseUrl("https://api.soundcloud.com/")
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build();
}
示例15: 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();
}