Java Code Examples for com.squareup.okhttp.OkHttpClient#setReadTimeout()

The following examples show how to use com.squareup.okhttp.OkHttpClient#setReadTimeout() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OkHttpClientFactory.java    From Auth0.Android with MIT License 6 votes vote down vote up
@VisibleForTesting
OkHttpClient modifyClient(OkHttpClient client, boolean loggingEnabled, boolean tls12Enforced, int connectTimeout, int readTimeout, int writeTimeout) {
    if (loggingEnabled) {
        enableLogging(client);
    }
    if (tls12Enforced) {
        enforceTls12(client);
    }
    if(connectTimeout > 0){
        client.setConnectTimeout(connectTimeout, TimeUnit.SECONDS);
    }
    if(readTimeout > 0){
        client.setReadTimeout(readTimeout, TimeUnit.SECONDS);
    }
    if(writeTimeout > 0){
        client.setWriteTimeout(writeTimeout, TimeUnit.SECONDS);
    }
    client.setProtocols(Arrays.asList(Protocol.HTTP_1_1, Protocol.SPDY_3));
    return client;
}
 
Example 2
Source File: HttpUtil.java    From AutoEx with Apache License 2.0 6 votes vote down vote up
public static void dogetHttp3(final String urls, final HResponse mHResponse) {
    try {
        OkHttpClient client = new OkHttpClient();
        client.setConnectTimeout(10, TimeUnit.SECONDS);
        client.setReadTimeout(60, TimeUnit.SECONDS);
        Request build1 = new Request.Builder().url(urls).get().build();

        Response execute = client.newCall(build1).execute();
        if (execute == null || execute.body() == null) {
            mHResponse.onError("没有找到任何可参考的,真可惜。");
            return;
        }
        String string = execute.body().string();
        mHResponse.onFinish(string);
    } catch (IOException e) {
        e.printStackTrace();
        mHResponse.onError(e.getMessage());
    }
}
 
Example 3
Source File: ApiConnection.java    From Android-CleanArchitecture with Apache License 2.0 5 votes vote down vote up
private OkHttpClient createClient() {
  final OkHttpClient okHttpClient = new OkHttpClient();
  okHttpClient.setReadTimeout(10000, TimeUnit.MILLISECONDS);
  okHttpClient.setConnectTimeout(15000, TimeUnit.MILLISECONDS);

  return okHttpClient;
}
 
Example 4
Source File: WXOkHttpDispatcher.java    From weex with Apache License 2.0 5 votes vote down vote up
private static OkHttpClient defaultOkHttpClient() {
  OkHttpClient client = new OkHttpClient();
  client.networkInterceptors().add(new OkHttpInterceptor());
  client.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
  client.setReadTimeout(DEFAULT_READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
  client.setWriteTimeout(DEFAULT_WRITE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
  return client;
}
 
Example 5
Source File: TimeoutClientProvider.java    From infobip-api-java-client with Apache License 2.0 5 votes vote down vote up
@Override
public Client get() {
	final OkHttpClient okHttpClient = new OkHttpClient();
	okHttpClient.setReadTimeout(configuration.getReadTimeout(), TimeUnit.MILLISECONDS);
	okHttpClient.setConnectTimeout(configuration.getConnectionTimeout(), TimeUnit.MILLISECONDS);
	return new OkClient(okHttpClient);
}
 
Example 6
Source File: NetworkModule.java    From Dagger-2-Example with Apache License 2.0 5 votes vote down vote up
@Provides
@Singleton
public OkHttpClient provideClient(Integer networkTimeout) {
    OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.setConnectTimeout(networkTimeout, TimeUnit.SECONDS);
    okHttpClient.setReadTimeout(networkTimeout, TimeUnit.SECONDS);
    return okHttpClient;
}
 
Example 7
Source File: RepoManager.java    From dhis2-android-dashboard with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static OkHttpClient provideOkHttpClient(Credentials credentials, Context context) {

        OkHttpClient client = new OkHttpClient();
        client.interceptors().add(provideInterceptor(credentials));
        client.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
        client.setReadTimeout(DEFAULT_READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
        client.setWriteTimeout(DEFAULT_WRITE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
        client.setCache(provideCache(context));
        return client;
    }
 
Example 8
Source File: ApiConnection.java    From Rocko-Android-Demos with Apache License 2.0 5 votes vote down vote up
private OkHttpClient createClient() {
  final OkHttpClient okHttpClient = new OkHttpClient();
  okHttpClient.setReadTimeout(10000, TimeUnit.MILLISECONDS);
  okHttpClient.setConnectTimeout(15000, TimeUnit.MILLISECONDS);

  return okHttpClient;
}
 
Example 9
Source File: Downloader.java    From external-resources with Apache License 2.0 5 votes vote down vote up
public Downloader(@NonNull Context context, @NonNull OkHttpClient client,
    @NonNull Converter converter, @NonNull Url url, @NonNull Options options) {
  this.context = context.getApplicationContext();
  this.client = client;
  this.url = url;
  this.options = options;
  this.converter = converter;

  Cache cache = new Cache(context.getApplicationContext());

  client.setConnectTimeout(CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
  client.setReadTimeout(READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
  client.setWriteTimeout(WRITE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
  client.setCache(new com.squareup.okhttp.Cache(cache.getCacheDir(), cache.getCacheSize()));
}
 
Example 10
Source File: OkHttpRequestor.java    From dropbox-sdk-java with MIT License 5 votes vote down vote up
/**
 * Returns an {@code OkHttpClient} instance with the default settings for this SDK.
 */
public static OkHttpClient defaultOkHttpClient() {
    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    client.setReadTimeout(DEFAULT_READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    client.setWriteTimeout(DEFAULT_READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    // enables certificate pinning
    client.setSslSocketFactory(SSLConfig.getSSLSocketFactory());
    return client;
}
 
Example 11
Source File: ApiFactory.java    From spark-sdk-android with Apache License 2.0 5 votes vote down vote up
private static OkHttpClient buildClientWithTimeout(int timeoutInSeconds) {
    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(timeoutInSeconds, TimeUnit.SECONDS);
    client.setReadTimeout(timeoutInSeconds, TimeUnit.SECONDS);
    client.setWriteTimeout(timeoutInSeconds, TimeUnit.SECONDS);
    return client;
}
 
Example 12
Source File: NightscoutUploader.java    From xDrip-Experimental with GNU General Public License v3.0 5 votes vote down vote up
public NightscoutUploader(Context context) {
    mContext = context;
    prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
    client = new OkHttpClient();
    client.setConnectTimeout(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
    client.setWriteTimeout(SOCKET_TIMEOUT, TimeUnit.MILLISECONDS);
    client.setReadTimeout(SOCKET_TIMEOUT, TimeUnit.MILLISECONDS);
    enableRESTUpload = prefs.getBoolean("cloud_storage_api_enable", false);
    enableMongoUpload = prefs.getBoolean("cloud_storage_mongodb_enable", false);
}
 
Example 13
Source File: RemoteService.java    From AndroidNetwork with MIT License 5 votes vote down vote up
public static synchronized RemoteService getInstance() {
    if (service == null) {
        service = new RemoteService();
        mOkHttpClient = new OkHttpClient();

        //设置超时时间
        //参见:OkHttp3超时设置和超时异常捕获
        //http://blog.csdn.net/do168/article/details/51848895
        mOkHttpClient.setConnectTimeout(10, TimeUnit.SECONDS);
        mOkHttpClient.setWriteTimeout(10, TimeUnit.SECONDS);
        mOkHttpClient.setReadTimeout(30, TimeUnit.SECONDS);
    }
    return service;
}
 
Example 14
Source File: RemoteService.java    From AndroidNetwork with MIT License 5 votes vote down vote up
public static synchronized RemoteService getInstance() {
    if (service == null) {
        service = new RemoteService();
        mOkHttpClient = new OkHttpClient();

        //设置超时时间
        //参见:OkHttp3超时设置和超时异常捕获
        //http://blog.csdn.net/do168/article/details/51848895
        mOkHttpClient.setConnectTimeout(10, TimeUnit.SECONDS);
        mOkHttpClient.setWriteTimeout(10, TimeUnit.SECONDS);
        mOkHttpClient.setReadTimeout(30, TimeUnit.SECONDS);
    }
    return service;
}
 
Example 15
Source File: SwaggerHubClient.java    From swaggerhub-maven-plugin with Apache License 2.0 5 votes vote down vote up
private OkHttpClient setupHttpClient(){
    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(30, TimeUnit.SECONDS);
    client.setReadTimeout(30, TimeUnit.SECONDS);
    client.setWriteTimeout(30, TimeUnit.SECONDS);
    return client;
}
 
Example 16
Source File: DOkHttp.java    From Pas with Apache License 2.0 5 votes vote down vote up
private DOkHttp() {
    mOkHttpClient = new OkHttpClient();
    // mOkHttpClient.networkInterceptors().add(new StethoInterceptor());

    mOkHttpClient.setConnectTimeout(10, TimeUnit.SECONDS);
    mOkHttpClient.setWriteTimeout(20, TimeUnit.SECONDS);
    mOkHttpClient.setReadTimeout(30, TimeUnit.SECONDS);

    gson=new Gson();

    //更新UI线程
    mainHanlder = new Handler(Looper.getMainLooper());

}
 
Example 17
Source File: ApiClient.java    From githot with Apache License 2.0 5 votes vote down vote up
/**
 * 用于Stethoscope调试的ttpClient
 */
public static OkClient getOkClient() {
    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    client.setReadTimeout(READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    client.networkInterceptors().add(new StethoInterceptor());
    return new OkClient(client);
}
 
Example 18
Source File: InvokeHTTP.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@OnScheduled
public void setUpClient(final ProcessContext context) throws IOException {
    okHttpClientAtomicReference.set(null);

    OkHttpClient okHttpClient = new OkHttpClient();

    // Add a proxy if set
    final String proxyHost = context.getProperty(PROP_PROXY_HOST).getValue();
    final Integer proxyPort = context.getProperty(PROP_PROXY_PORT).asInteger();
    if (proxyHost != null && proxyPort != null) {
        final Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
        okHttpClient.setProxy(proxy);
    }

    // Set timeouts
    okHttpClient.setConnectTimeout((context.getProperty(PROP_CONNECT_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue()), TimeUnit.MILLISECONDS);
    okHttpClient.setReadTimeout(context.getProperty(PROP_READ_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue(), TimeUnit.MILLISECONDS);

    // Set whether to follow redirects
    okHttpClient.setFollowRedirects(context.getProperty(PROP_FOLLOW_REDIRECTS).asBoolean());

    final SSLContextService sslService = context.getProperty(PROP_SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class);
    final SSLContext sslContext = sslService == null ? null : sslService.createSSLContext(ClientAuth.NONE);

    // check if the ssl context is set and add the factory if so
    if (sslContext != null) {
        okHttpClient.setSslSocketFactory(sslContext.getSocketFactory());
    }

    // check the trusted hostname property and override the HostnameVerifier
    String trustedHostname = trimToEmpty(context.getProperty(PROP_TRUSTED_HOSTNAME).getValue());
    if (!trustedHostname.isEmpty()) {
        okHttpClient.setHostnameVerifier(new OverrideHostnameVerifier(trustedHostname, okHttpClient.getHostnameVerifier()));
    }

    setAuthenticator(okHttpClient, context);

    useChunked = context.getProperty(PROP_USE_CHUNKED_ENCODING).asBoolean();

    okHttpClientAtomicReference.set(okHttpClient);
}
 
Example 19
Source File: API.java    From ADP with MIT License 4 votes vote down vote up
private Client getHttpClient() {
    OkHttpClient httpClient = new OkHttpClient();
    httpClient.setConnectTimeout(Config.API_HTTP_CONNECT_TIMEOUT, TimeUnit.MILLISECONDS);
    httpClient.setReadTimeout(Config.API_HTTP_READ_TIMEOUT, TimeUnit.MILLISECONDS);
    return new OkClient(httpClient);
}
 
Example 20
Source File: OkHttpHelper.java    From ImitateTaobaoApp with Apache License 2.0 3 votes vote down vote up
private OkHttpHelper(){

        mHttpClient = new OkHttpClient();

        mHttpClient.setConnectTimeout(10, TimeUnit.SECONDS);
        mHttpClient.setReadTimeout(10,TimeUnit.SECONDS);
        mHttpClient.setWriteTimeout(30,TimeUnit.SECONDS);

        mGson = new Gson();

        mHandler = new Handler(Looper.getMainLooper()); //主要接受子线程发送的数据, 并用此数据配合主线程
        // 更新UI

    }