Example usage for com.squareup.okhttp OkHttpClient setWriteTimeout

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

Introduction

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

Prototype

public void setWriteTimeout(long timeout, TimeUnit unit) 

Source Link

Document

Sets the default write timeout for new connections.

Usage

From source file:org.wso2.carbon.identity.authenticator.duo.DuoHttp.java

License:Open Source License

public Response executeHttpRequest() throws Exception {
    String url = "https://" + host + uri;
    String queryString = createQueryString();
    Request.Builder builder = new Request.Builder();
    if (method.equals("POST")) {
        builder.post(RequestBody.create(FORM_ENCODED, queryString));
    } else if (method.equals("PUT")) {
        builder.put(RequestBody.create(FORM_ENCODED, queryString));
    } else if (method.equals("GET")) {
        if (queryString.length() > 0) {
            url += "?" + queryString;
        }/*from   w ww . j  a  v  a  2  s .c om*/
        builder.get();
    } else if (method.equals("DELETE")) {
        if (queryString.length() > 0) {
            url += "?" + queryString;
        }
        builder.delete();
    } else {
        throw new UnsupportedOperationException("Unsupported method: " + method);
    }
    Request request = builder.url(url).build();
    // Set up client.
    OkHttpClient httpclient = new OkHttpClient();
    if (proxy != null) {
        httpclient.setProxy(proxy);
    }
    httpclient.setConnectTimeout(timeout, TimeUnit.SECONDS);
    httpclient.setWriteTimeout(timeout, TimeUnit.SECONDS);
    httpclient.setReadTimeout(timeout, TimeUnit.SECONDS);
    // finish and execute request
    builder.headers(headers.build());
    return httpclient.newCall(builder.build()).execute();
}

From source file:retrofit.MagnetRestAdapter.java

License:Apache License

/**
 * Get the OkHttpClient with proper timeout settings
 * @param method//from w w w.jav  a  2  s  .  c o m
 * @return
 */
private OkHttpClient getClient(Method method) {
    Timeout timeoutAnnotation = method.getAnnotation(Timeout.class);
    if (null != timeoutAnnotation) {
        if (0 != timeoutAnnotation.read() || 0 != timeoutAnnotation.write()) {
            OkHttpClient newClient = client.clone();
            if (isTimeoutValid(timeoutAnnotation.read(), method, timeoutAnnotation)) {
                newClient.setReadTimeout(timeoutAnnotation.read(), TimeUnit.SECONDS);
            }
            if (isTimeoutValid(timeoutAnnotation.write(), method, timeoutAnnotation)) {
                newClient.setWriteTimeout(timeoutAnnotation.write(), TimeUnit.SECONDS);
            }

            Log.d(TAG,
                    "Using new OkHttpClient with new timeout " + timeoutAnnotation + " for method " + method);

            return newClient;
        }
    }

    return client;
}

From source file:retrofit.Platform.java

License:Apache License

OkHttpClient defaultClient() {
    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(15, TimeUnit.SECONDS);
    client.setReadTimeout(15, TimeUnit.SECONDS);
    client.setWriteTimeout(15, TimeUnit.SECONDS);
    return client;
}

From source file:zblibrary.demo.manager.HttpRequest.java

License:Apache License

/**
 * @param url//from   w w  w. jav a  2s.co  m
 * @return
 */
private OkHttpClient getHttpClient(String url) {
    Log.i(TAG, "getHttpClient  url = " + url);
    if (StringUtil.isNotEmpty(url, true) == false) {
        Log.e(TAG, "getHttpClient  StringUtil.isNotEmpty(url, true) == false >> return null;");
        return null;
    }

    OkHttpClient client = new OkHttpClient();
    client.setCookieHandler(new HttpHead());
    client.setConnectTimeout(15, TimeUnit.SECONDS);
    client.setWriteTimeout(10, TimeUnit.SECONDS);
    client.setReadTimeout(10, TimeUnit.SECONDS);
    //https?,??,???
    if (url.startsWith(StringUtil.URL_PREFIXs) && socketFactory != null) {
        client.setSslSocketFactory(socketFactory);
    }

    return client;
}