Example usage for com.squareup.okhttp OkHttpClient setConnectTimeout

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

Introduction

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

Prototype

public void setConnectTimeout(long timeout, TimeUnit unit) 

Source Link

Document

Sets the default connect timeout for new connections.

Usage

From source file:apijson.demo.client.manager.HttpManager.java

License:Apache License

/**
 * @param url//from w w w  . j a va  2  s  .  c  o  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);

    return client;
}

From source file:app.philm.in.network.PhilmTmdb.java

License:Apache License

@Override
protected RestAdapter.Builder newRestAdapterBuilder() {
    RestAdapter.Builder b = super.newRestAdapterBuilder();

    if (mCacheLocation != null) {
        OkHttpClient client = new OkHttpClient();

        try {//from ww  w .  j a  v  a2 s  .c  om
            File cacheDir = new File(mCacheLocation, UUID.randomUUID().toString());
            Cache cache = new Cache(cacheDir, 1024);
            client.setCache(cache);
        } catch (IOException e) {
            Log.e(TAG, "Could not use OkHttp Cache", e);
        }

        client.setConnectTimeout(Constants.CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
        client.setReadTimeout(Constants.READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);

        b.setClient(new OkClient(client));
    }

    return b;
}

From source file:augsburg.se.alltagsguide.ServicesModule.java

License:Open Source License

@Singleton
@Provides/*from  w w  w .ja v a2  s .co m*/
OkHttpClient okHttpClient(Context context, @Named("cacheDir") File cachedir) {
    Ln.d("okHttpClient is intialized.");
    int cacheSize = 50 * 1024 * 1024; // 50 MiB
    Cache cache = new Cache(cachedir, cacheSize);
    OkHttpClient client = new OkHttpClient();
    client.setCache(cache);
    client.setConnectTimeout(10, TimeUnit.SECONDS);
    client.setWriteTimeout(10, TimeUnit.SECONDS);
    client.setReadTimeout(30, TimeUnit.SECONDS);
    return client;
}

From source file:br.com.mobiplus.flickr.rest.RetrofitFacade.java

public RetrofitFacade(Context context) {
    this.context = context.getApplicationContext();
    this.requestInterceptor = new RetrofitRequestInterceptor(context);

    final OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.setReadTimeout(60, TimeUnit.SECONDS);
    okHttpClient.setConnectTimeout(30, TimeUnit.SECONDS);

    this.gsonRestAdapter = new RestAdapter.Builder().setLogLevel(RestAdapter.LogLevel.FULL)
            .setLog(new AndroidLog("##FLICKR_CLIENT")).setEndpoint(SERVER_ENDPOINT)
            .setRequestInterceptor(requestInterceptor).setClient(new OkClient(okHttpClient)).build();

    this.service = gsonRestAdapter.create(RetrofitUrlMapping.class);
}

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

License:Apache License

public static OkHttpClient getOkHttpClientFactory(long timeout) {
    OkHttpClient client = new OkHttpClient();
    ///*from w ww  . ja  va 2 s. c  om*/
    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.adnanbal.fxdedektifi.sample.data.net.ApiConnection.java

License:Apache License

private OkHttpClient createClient() {
    final OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.setReadTimeout(10000, TimeUnit.MILLISECONDS);
    okHttpClient.setConnectTimeout(15000, TimeUnit.MILLISECONDS);

    return okHttpClient;
}

From source file:com.aix.city.comm.OkHttpStack.java

License:Open Source License

@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
        throws IOException, AuthFailureError {

    OkHttpClient client = mClient.clone();
    int timeoutMs = request.getTimeoutMs();
    client.setConnectTimeout(timeoutMs, TimeUnit.MILLISECONDS);
    client.setReadTimeout(timeoutMs, TimeUnit.MILLISECONDS);
    client.setWriteTimeout(timeoutMs, TimeUnit.MILLISECONDS);

    com.squareup.okhttp.Request.Builder okHttpRequestBuilder = new com.squareup.okhttp.Request.Builder();
    okHttpRequestBuilder.url(request.getUrl());

    Map<String, String> headers = request.getHeaders();
    for (final String name : headers.keySet()) {
        okHttpRequestBuilder.addHeader(name, headers.get(name));
    }/*from  ww w . java  2 s .c  om*/
    for (final String name : additionalHeaders.keySet()) {
        okHttpRequestBuilder.addHeader(name, additionalHeaders.get(name));
    }

    setConnectionParametersForRequest(okHttpRequestBuilder, request);

    com.squareup.okhttp.Request okHttpRequest = okHttpRequestBuilder.build();
    Call okHttpCall = client.newCall(okHttpRequest);
    Response okHttpResponse = okHttpCall.execute();

    StatusLine responseStatus = new BasicStatusLine(parseProtocol(okHttpResponse.protocol()),
            okHttpResponse.code(), okHttpResponse.message());
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    response.setEntity(entityFromOkHttpResponse(okHttpResponse));

    Headers responseHeaders = okHttpResponse.headers();
    for (int i = 0, len = responseHeaders.size(); i < len; i++) {
        final String name = responseHeaders.name(i), value = responseHeaders.value(i);
        if (name != null) {
            response.addHeader(new BasicHeader(name, value));
        }
    }

    return response;
}

From source file:com.appstarter.utils.WebUtils.java

License:Apache License

public static String doHttpGet(String url) throws AppStarterException {
    if (BuildConfig.DEBUG) {
        Log.d(TAG, "doHttpGet - url: " + url);
    }/*  w w  w .j av a2 s  .co m*/

    String ret = "";

    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(15, TimeUnit.SECONDS); // connect timeout
    client.setReadTimeout(15, TimeUnit.SECONDS); // socket timeout

    try {
        Request request = new Request.Builder().url(new URL(url))
                //               .header("User-Agent", "OkHttp Headers.java")
                //               .addHeader("Accept", "application/json; q=0.5")
                .build();

        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) {
            String debugMessage = "doHttpGet - OkHttp.Response is not successful - " + response.message() + " ("
                    + response.code() + ")";
            throw new AppStarterException(AppStarterException.ERROR_SERVER, debugMessage);
        }
        ret = response.body().string();
    } catch (IOException e) {
        throw new AppStarterException(e, AppStarterException.ERROR_NETWORK_GET);
    }

    return ret;
}

From source file:com.appstarter.utils.WebUtils.java

License:Apache License

public static String doHttpPost(String url, List<NameValuePair> params) throws AppStarterException {
    if (BuildConfig.DEBUG) {
        Log.d(TAG, "doHttpPost - url: " + debugRequest(url, params));
    }/*from www . ja  v a  2  s. com*/

    String ret = "";

    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(15, TimeUnit.SECONDS); // connect timeout
    client.setReadTimeout(15, TimeUnit.SECONDS); // socket timeout

    FormEncodingBuilder builder = new FormEncodingBuilder();
    for (NameValuePair nvp : params) {
        builder.add(nvp.getName(), nvp.getValue());
    }
    RequestBody formBody = builder.build();

    try {
        Request request = new Request.Builder().url(new URL(url))
                //               .header("User-Agent", "OkHttp Headers.java")
                //               .addHeader("Accept", "application/json; q=0.5")
                .post(formBody).build();

        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) {
            String debugMessage = "doHttpPost - OkHttp.Response is not successful - " + response.message()
                    + " (" + response.code() + ")";
            throw new AppStarterException(AppStarterException.ERROR_SERVER, debugMessage);
        }
        ret = response.body().string();
    } catch (IOException e) {
        throw new AppStarterException(e, AppStarterException.ERROR_NETWORK_GET);
    }

    return ret;
}

From source file:com.arnaudpiroelle.marvel.api.rest.client.RetrofitHttpClient.java

License:Apache License

private static OkUrlFactory generateDefaultOkUrlFactory() {
    OkHttpClient client = new com.squareup.okhttp.OkHttpClient();
    client.setConnectTimeout(CONNECT_TIMEOUT_SEC, TimeUnit.SECONDS);
    client.setReadTimeout(READ_TIMEOUT_SEC, TimeUnit.SECONDS);

    try {/*from  w w w  .  j  a v  a  2 s .  co m*/
        File cacheDir = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
        Cache cache = new Cache(cacheDir, 1024);
        client.setCache(cache);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return new OkUrlFactory(client);
}