Java Code Examples for okhttp3.OkHttpClient.Builder#retryOnConnectionFailure()

The following examples show how to use okhttp3.OkHttpClient.Builder#retryOnConnectionFailure() . 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: OKHttpFactory.java    From flower with Apache License 2.0 6 votes vote down vote up
private void initOKHttp() {
  Builder builder = new OkHttpClient().newBuilder();
  builder.connectTimeout(httpConfig.getConnectTimeout(), TimeUnit.MILLISECONDS);
  builder.writeTimeout(httpConfig.getWriteTimeout(), TimeUnit.MILLISECONDS);
  builder.readTimeout(httpConfig.getReadTimeout(), TimeUnit.MILLISECONDS);

  ExecutorService executorService = new ThreadPoolExecutor(8, Runtime.getRuntime().availableProcessors() * 8, 60,
      TimeUnit.SECONDS, new SynchronousQueue<>(), new NamedThreadFactory("flower-http"));

  Dispatcher dispatcher = new Dispatcher(executorService);
  dispatcher.setMaxRequests(256);
  dispatcher.setMaxRequestsPerHost(Math.max(32, Runtime.getRuntime().availableProcessors() * 8));

  builder.dispatcher(dispatcher);
  builder.retryOnConnectionFailure(true);
  builder.connectionPool(new ConnectionPool());

  this.template = builder.build();
}
 
Example 2
Source File: CurlThread.java    From fiery with Apache License 2.0 4 votes vote down vote up
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;
    */
}