Example usage for com.squareup.okhttp OkHttpClient setRetryOnConnectionFailure

List of usage examples for com.squareup.okhttp OkHttpClient setRetryOnConnectionFailure

Introduction

In this page you can find the example usage for com.squareup.okhttp OkHttpClient setRetryOnConnectionFailure.

Prototype

public void setRetryOnConnectionFailure(boolean retryOnConnectionFailure) 

Source Link

Document

Configure this client to retry or not when a connectivity problem is encountered.

Usage

From source file:cn.finalteam.okhttpfinal.OkHttpFactory.java

License:Apache License

public static OkHttpClient getOkHttpClientFactory(long timeout) {
    OkHttpClient client = new OkHttpClient();
    ////  w  w w . jav  a2  s  . com
    client.setConnectTimeout(timeout, TimeUnit.MILLISECONDS);
    client.setWriteTimeout(timeout, TimeUnit.MILLISECONDS);
    client.setReadTimeout(timeout, TimeUnit.MILLISECONDS);
    //???
    client.setRetryOnConnectionFailure(false);
    //???
    client.setFollowRedirects(true);
    //?cookie
    client.setCookieHandler(new CookieManager(null, CookiePolicy.ACCEPT_ORIGINAL_SERVER));

    return client;
}

From source file:com.netflix.spinnaker.echo.config.Front50Config.java

License:Apache License

@Bean
public OkHttpClient okHttpClient(OkHttpClientConfiguration okHttpClientConfig) {
    OkHttpClient cli = okHttpClientConfig.create();
    cli.setConnectionPool(new ConnectionPool(maxIdleConnections, keepAliveDurationMs));
    cli.setRetryOnConnectionFailure(retryOnConnectionFailure);
    return cli;/*ww w  .j av  a  2  s .  c om*/
}

From source file:com.netflix.spinnaker.halyard.config.config.v1.RetrofitConfig.java

License:Apache License

@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
OkClient okClient() {/*from  w w w .ja  v a  2  s .  c  om*/
    OkHttpClient client = okHttpClientConfig.create();
    client.setConnectionPool(new ConnectionPool(maxIdleConnections, keepAliveDurationMs));
    client.setRetryOnConnectionFailure(retryOnConnectionFailure);
    return new OkClient(client);
}

From source file:keywhiz.cli.ClientUtils.java

License:Apache License

/**
 * Creates a {@link OkHttpClient} to start a TLS connection.
 *
 * @param devTrustStore if not null, uses the provided TrustStore instead of whatever is
 *                      configured in the JVM. This is a convenient way to allow developers to
 *                      start playing with Keywhiz right away. This option should not be used in
 *                      production systems.
 * @param cookies list of cookies to include in the client.
 * @return new http client.//from  w  w  w . j  a  va 2 s. c  o  m
 */
public static OkHttpClient sslOkHttpClient(@Nullable KeyStore devTrustStore, List<HttpCookie> cookies) {
    checkNotNull(cookies);

    SSLContext sslContext;
    try {
        TrustManagerFactory trustManagerFactory = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());

        trustManagerFactory.init(devTrustStore);

        TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();

        sslContext = SSLContext.getInstance("TLSv1.2");
        sslContext.init(new KeyManager[0], trustManagers, new SecureRandom());
    } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
        throw Throwables.propagate(e);
    }

    SSLSocketFactory socketFactory = sslContext.getSocketFactory();

    OkHttpClient client = new OkHttpClient().setSslSocketFactory(socketFactory)
            .setConnectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS)).setFollowSslRedirects(false);

    client.setRetryOnConnectionFailure(false);
    client.networkInterceptors().add(new XsrfTokenInterceptor("XSRF-TOKEN", "X-XSRF-TOKEN"));

    CookieManager cookieManager = new CookieManager();
    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    cookies.forEach(c -> cookieManager.getCookieStore().add(null, c));

    client.setCookieHandler(cookieManager);
    return client;
}

From source file:org.eyeseetea.malariacare.network.NetworkUtils.java

License:Open Source License

/**
 * Pushes data to DHIS Server/*  w w  w  .  j a v a2 s  .  c  om*/
 * @param data
 */
public JSONObject pushData(JSONObject data) throws Exception {
    Response response = null;

    final String DHIS_URL = getDhisURL() + DHIS_PUSH_API;

    OkHttpClient client = UnsafeOkHttpsClientFactory.getUnsafeOkHttpClient();
    client.setConnectTimeout(30, TimeUnit.SECONDS); // connect timeout
    client.setReadTimeout(30, TimeUnit.SECONDS); // socket timeout
    client.setWriteTimeout(30, TimeUnit.SECONDS); // write timeout
    client.setRetryOnConnectionFailure(false); // Cancel retry on failure
    BasicAuthenticator basicAuthenticator = new BasicAuthenticator();
    client.setAuthenticator(basicAuthenticator);

    Log.d(TAG, "Url" + DHIS_URL + "");
    RequestBody body = RequestBody.create(JSON, data.toString());
    Request request = new Request.Builder()
            .header(basicAuthenticator.AUTHORIZATION_HEADER, basicAuthenticator.getCredentials()).url(DHIS_URL)
            .post(body).build();

    response = client.newCall(request).execute();
    if (!response.isSuccessful()) {
        Log.e(TAG, "pushData (" + response.code() + "): " + response.body().string());
        throw new IOException(response.message());
    }
    return parseResponse(response.body().string());
}

From source file:org.eyeseetea.malariacare.network.NetworkUtils.java

License:Open Source License

/**
 * Call to DHIS Server/*from  w w  w  . jav  a 2 s . co m*/
 * @param data
 * @param url
 */
public Response executeCall(JSONObject data, String url, String method) throws IOException {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext);
    final String DHIS_URL = sharedPreferences.getString(applicationContext.getString(R.string.dhis_url),
            applicationContext.getString(R.string.login_info_dhis_default_server_url)) + url;

    OkHttpClient client = UnsafeOkHttpsClientFactory.getUnsafeOkHttpClient();

    client.setConnectTimeout(30, TimeUnit.SECONDS); // connect timeout
    client.setReadTimeout(30, TimeUnit.SECONDS); // socket timeout
    client.setWriteTimeout(30, TimeUnit.SECONDS); // write timeout
    client.setRetryOnConnectionFailure(false); // Cancel retry on failure

    BasicAuthenticator basicAuthenticator = new BasicAuthenticator();
    client.setAuthenticator(basicAuthenticator);

    Request.Builder builder = new Request.Builder()
            .header(basicAuthenticator.AUTHORIZATION_HEADER, basicAuthenticator.getCredentials()).url(DHIS_URL);

    switch (method) {
    case "POST":
        RequestBody postBody = RequestBody.create(JSON, data.toString());
        builder.post(postBody);
        break;
    case "PUT":
        RequestBody putBody = RequestBody.create(JSON, data.toString());
        builder.put(putBody);
        break;
    case "PATCH":
        RequestBody patchBody = RequestBody.create(JSON, data.toString());
        builder.patch(patchBody);
        break;
    case "GET":
        builder.get();
        break;
    }

    Request request = builder.build();
    return client.newCall(request).execute();
}

From source file:org.graylog2.shared.bindings.providers.OkHttpClientProvider.java

License:Open Source License

@Override
public OkHttpClient get() {
    final OkHttpClient client = new OkHttpClient();
    client.setRetryOnConnectionFailure(true);
    client.setConnectTimeout(connectTimeout.getQuantity(), connectTimeout.getUnit());
    client.setWriteTimeout(writeTimeout.getQuantity(), writeTimeout.getUnit());
    client.setReadTimeout(readTimeout.getQuantity(), readTimeout.getUnit());

    if (httpProxyUri != null) {
        final Proxy proxy = new Proxy(Proxy.Type.HTTP,
                new InetSocketAddress(httpProxyUri.getHost(), httpProxyUri.getPort()));
        client.setProxy(proxy);/* w w  w  .j  av a  2 s  . c o m*/
    }

    return client;
}