Example usage for com.squareup.okhttp OkHttpClient setReadTimeout

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

Introduction

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

Prototype

public void setReadTimeout(long timeout, TimeUnit unit) 

Source Link

Document

Sets the default read timeout for new connections.

Usage

From source file:com.ushahidi.android.presentation.di.modules.AppModule.java

License:Open Source License

/**
 * Provides {@link HttpClientWrap} object
 *
 * @param context The calling context//from  w  ww .  j ava 2s  . c  o  m
 * @return The http client wrap object
 */
@Provides
@Singleton
HttpClientWrap provideOkHttpClient(Context context) {
    final OkHttpClient okHttpClient = createOkHttpClient(context.getApplicationContext());
    okHttpClient.setCookieHandler(CookieHandler.getDefault());
    okHttpClient.setConnectTimeout(10, TimeUnit.SECONDS);
    okHttpClient.setReadTimeout(10, TimeUnit.SECONDS);
    okHttpClient.setWriteTimeout(10, TimeUnit.SECONDS);
    return new HttpClientWrap(context, new OkClient(okHttpClient));
}

From source file:com.uwetrottmann.getglue.Utils.java

License:Apache License

/**
 * Create an OkHttpClient with sensible timeouts for mobile connections.
 *//*www .ja v a  2 s .com*/
public static OkHttpClient createOkHttpClient() {
    OkHttpClient okHttpClient = new OkHttpClient();

    // set timeouts
    okHttpClient.setConnectTimeout(15 * 1000, TimeUnit.MILLISECONDS);
    okHttpClient.setReadTimeout(20 * 1000, TimeUnit.MILLISECONDS);

    return okHttpClient;
}

From source file:com.vaporwarecorp.mirror.app.MirrorApplication.java

License:Apache License

private void initializeGlide() {
    Cache cache = new Cache(new File(getCacheDir(), "http"), 25 * 1024 * 1024);

    OkHttpClient mOkHttpClient = new OkHttpClient();
    mOkHttpClient.setCache(cache);/* ww w .ja  v a2  s .  c o m*/
    mOkHttpClient.setConnectTimeout(10, SECONDS);
    mOkHttpClient.setReadTimeout(10, SECONDS);
    mOkHttpClient.setWriteTimeout(10, SECONDS);

    Glide.get(getApplicationContext()).register(GlideUrl.class, InputStream.class,
            new OkHttpUrlLoader.Factory(mOkHttpClient));
}

From source file:com.veaer.gank.request.LineRetrofit.java

License:Open Source License

LineRetrofit() {
    OkHttpClient client = new OkHttpClient();
    client.setReadTimeout(12, TimeUnit.SECONDS);
    int cacheSize = 10 * 1024 * 1024; // 10 MiB
    cache = new Cache(FileUtils.getHttpCacheDir(), cacheSize);
    client.setCache(cache);/*from w w  w  .  j a v  a2 s  . com*/
    client.networkInterceptors().add(new CacheInterceptor());

    RestAdapter restAdapter = new RestAdapter.Builder().setClient(new OkClient(client))
            .setEndpoint("http://gank.avosapps.com/api/").setConverter(new GsonConverter(gson)).build();
    service = restAdapter.create(Line.class);
}

From source file:com.wialon.remote.OkSdkHttpClient.java

License:Apache License

public OkHttpClient getHttpClient(int timeout) {
    if ((timeout == 0 || timeout == DEFAULT_SOCKET_TIMEOUT) && defaultClient != null)
        return defaultClient;
    else {/*  www  . j a  v  a  2  s.  c o  m*/
        OkHttpClient client = new OkHttpClient();
        timeout = timeout == 0 ? DEFAULT_SOCKET_TIMEOUT : timeout;
        client.setConnectTimeout(timeout, TimeUnit.MILLISECONDS);
        client.setWriteTimeout(timeout, TimeUnit.MILLISECONDS);
        client.setReadTimeout(timeout, TimeUnit.MILLISECONDS);
        return client;
    }
}

From source file:com.windigo.http.client.OkClient.java

License:Apache License

private static OkHttpClient generateDefaultOkHttp() {
    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(GlobalSettings.CONNNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
    client.setReadTimeout(GlobalSettings.CONNECTION_READ_TIMEOUT, TimeUnit.MILLISECONDS);
    return client;
}

From source file:com.yandex.disk.rest.OkHttpClientFactory.java

License:Apache License

public static OkHttpClient makeClient() {
    OkHttpClient client = new OkHttpClient();

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

    client.setFollowSslRedirects(true);//  www.j a  v  a 2s . c o m
    client.setFollowRedirects(true);

    return client;
}

From source file:com.ydh.gva.util.net.volley.toolbox.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();
    okHttpRequestBuilder.addHeader("clientos", "101");
    okHttpRequestBuilder.addHeader("osversion", SystemVal.sdk + "");
    okHttpRequestBuilder.addHeader("clientphone", SystemVal.model + "");
    okHttpRequestBuilder.addHeader("weiLeversion", SystemVal.versionCode + "");

    for (final String name : headers.keySet()) {
        okHttpRequestBuilder.addHeader(name, headers.get(name));
    }//from w w  w . j  a va2  s . c  o  m

    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.yetanotherdevblog.rottentomatoes.api.RottenTomatoes.java

License:Apache License

/**
 * Return the current {@link retrofit.RestAdapter} instance. If none exists (first call, API key changed),
 * builds a new one./* www  .j a va  2s . c  o m*/
 * <p>
 * When building, sets the endpoint, a custom converter ({@link RottenTomatoesHelper#getGsonBuilder()})
 * and a {@link retrofit.RequestInterceptor} which adds the API key as query param.
 */
protected RestAdapter getRestAdapter() {
    if (restAdapter == null) {
        RestAdapter.Builder builder = newRestAdapterBuilder();

        builder.setEndpoint(API_URL);
        builder.setConverter(new GsonConverter(RottenTomatoesHelper.getGsonBuilder().create()));
        builder.setRequestInterceptor(new RequestInterceptor() {
            public void intercept(RequestFacade requestFacade) {
                requestFacade.addQueryParam(PARAM_API_KEY, apiKey);
            }
        });

        OkHttpClient client = new OkHttpClient();
        client.setConnectTimeout(10, TimeUnit.SECONDS);
        client.setReadTimeout(10, TimeUnit.SECONDS);
        client.setWriteTimeout(10, TimeUnit.SECONDS);
        builder.setClient(new OkClient(client));

        if (isDebug) {
            builder.setLogLevel(RestAdapter.LogLevel.FULL);
        }

        restAdapter = builder.build();
    }

    return restAdapter;
}

From source file:de.schildbach.wallet.data.DynamicFeeLoader.java

License:Open Source License

private static void fetchDynamicFees(final HttpUrl url, final File tempFile, final File targetFile,
        final String userAgent) {
    final Stopwatch watch = Stopwatch.createStarted();

    final Request.Builder request = new Request.Builder();
    request.url(url);//from  www. j  ava 2 s  .  c o m
    request.header("User-Agent", userAgent);
    if (targetFile.exists())
        request.header("If-Modified-Since", HttpDate.format(new Date(targetFile.lastModified())));

    final OkHttpClient httpClient = Constants.HTTP_CLIENT.clone();
    httpClient.setConnectTimeout(5, TimeUnit.SECONDS);
    httpClient.setWriteTimeout(5, TimeUnit.SECONDS);
    httpClient.setReadTimeout(5, TimeUnit.SECONDS);
    final Call call = httpClient.newCall(request.build());
    try {
        final Response response = call.execute();
        final int status = response.code();
        if (status == HttpURLConnection.HTTP_NOT_MODIFIED) {
            log.info("Dynamic fees not modified at {}, took {}", url, watch);
        } else if (status == HttpURLConnection.HTTP_OK) {
            final ResponseBody body = response.body();
            final FileOutputStream os = new FileOutputStream(tempFile);
            Io.copy(body.byteStream(), os);
            os.close();
            final Date lastModified = response.headers().getDate("Last-Modified");
            if (lastModified != null)
                tempFile.setLastModified(lastModified.getTime());
            body.close();
            if (!tempFile.renameTo(targetFile))
                throw new IllegalStateException("Cannot rename " + tempFile + " to " + targetFile);
            watch.stop();
            log.info("Dynamic fees fetched from {}, took {}", url, watch);
        } else {
            log.warn("HTTP status {} when fetching dynamic fees from {}", response.code(), url);
        }
    } catch (final Exception x) {
        log.warn("Problem when fetching dynamic fees rates from " + url, x);
    }
}