Search in sources :

Example 6 with OkHttpClient

use of com.squareup.okhttp.OkHttpClient in project nmid-headline by miao1007.

the class RetrofitUtils method getCachedAdapter.

public static Retrofit getCachedAdapter(String endpoint) {
    Cache cache = null;
    OkHttpClient okHttpClient = null;
    Retrofit adapter;
    try {
        File cacheDir = new File(GlobalContext.getInstance().getCacheDir().getPath(), "pictures.json");
        cache = new Cache(cacheDir, 10 * 1024 * 1024);
        okHttpClient = new OkHttpClient();
        okHttpClient.setCache(cache);
    } catch (Exception e) {
        e.printStackTrace();
    }
    adapter = new Retrofit.Builder().baseUrl(endpoint).addConverterFactory(GsonConverterFactory.create()).client(okHttpClient).build();
    return adapter;
}
Also used : Retrofit(retrofit.Retrofit) OkHttpClient(com.squareup.okhttp.OkHttpClient) File(java.io.File) Cache(com.squareup.okhttp.Cache)

Example 7 with OkHttpClient

use of com.squareup.okhttp.OkHttpClient in project Timber by naman14.

the class RestServiceFactory method createStatic.

public static <T> T createStatic(final Context context, String baseUrl, Class<T> clazz) {
    final OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.setCache(new Cache(context.getApplicationContext().getCacheDir(), CACHE_SIZE));
    okHttpClient.setConnectTimeout(40, TimeUnit.SECONDS);
    RequestInterceptor interceptor = new RequestInterceptor() {

        @Override
        public void intercept(RequestFacade request) {
            //7-days cache
            request.addHeader("Cache-Control", String.format("max-age=%d,max-stale=%d", Integer.valueOf(60 * 60 * 24 * 7), Integer.valueOf(31536000)));
            request.addHeader("Connection", "keep-alive");
        }
    };
    RestAdapter.Builder builder = new RestAdapter.Builder().setEndpoint(baseUrl).setRequestInterceptor(interceptor).setClient(new OkClient(okHttpClient));
    return builder.build().create(clazz);
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) OkClient(retrofit.client.OkClient) RequestInterceptor(retrofit.RequestInterceptor) RestAdapter(retrofit.RestAdapter) Cache(com.squareup.okhttp.Cache)

Example 8 with OkHttpClient

use of com.squareup.okhttp.OkHttpClient in project SongkickInterview by pakoito.

the class NetworkModule method provideOkHttp.

@Provides
@Singleton
OkHttpClient provideOkHttp(final Cache cache, LoggerInterceptor loggerInterceptor, StethoInterceptor stethoInterceptor) {
    final OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.setCache(cache);
    okHttpClient.networkInterceptors().add(loggerInterceptor);
    okHttpClient.networkInterceptors().add(stethoInterceptor);
    okHttpClient.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    okHttpClient.setReadTimeout(DEFAULT_READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    okHttpClient.setWriteTimeout(DEFAULT_WRITE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    return okHttpClient;
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 9 with OkHttpClient

use of com.squareup.okhttp.OkHttpClient in project android-app by spark.

the class WebHelpers method disableTLSforStaging.

private static OkHttpClient disableTLSforStaging() {
    log.e("WARNING: TLS DISABLED FOR STAGING!");
    OkHttpClient client = new OkHttpClient();
    client.setHostnameVerifier(new HostnameVerifier() {

        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    });
    try {
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new X509TrustManager[] { new X509TrustManager() {

            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }

            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        } }, new SecureRandom());
        client.setSslSocketFactory(context.getSocketFactory());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return client;
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) X509TrustManager(javax.net.ssl.X509TrustManager) SSLSession(javax.net.ssl.SSLSession) SecureRandom(java.security.SecureRandom) CertificateException(java.security.cert.CertificateException) SSLContext(javax.net.ssl.SSLContext) X509Certificate(java.security.cert.X509Certificate) CertificateException(java.security.cert.CertificateException) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Example 10 with OkHttpClient

use of com.squareup.okhttp.OkHttpClient in project android-app by spark.

the class WebHelpers method initialize.

// should be called during Application.onCreate() to ensure availability
public static void initialize(Context ctx) {
    if (!initialized) {
        if (AppConfig.useStaging()) {
            okHttpClient = disableTLSforStaging();
        } else {
            okHttpClient = new OkHttpClient();
        }
        gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
        initialized = true;
    }
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder)

Aggregations

OkHttpClient (com.squareup.okhttp.OkHttpClient)47 Request (com.squareup.okhttp.Request)22 Response (com.squareup.okhttp.Response)18 IOException (java.io.IOException)15 RequestBody (com.squareup.okhttp.RequestBody)8 Provides (dagger.Provides)6 Gson (com.google.gson.Gson)5 Cache (com.squareup.okhttp.Cache)5 Singleton (javax.inject.Singleton)5 SpringAndroidSpiceRequest (com.octo.android.robospice.request.springandroid.SpringAndroidSpiceRequest)4 File (java.io.File)4 RestAdapter (retrofit.RestAdapter)4 OkClient (retrofit.client.OkClient)4 SSLContext (javax.net.ssl.SSLContext)3 X509TrustManager (javax.net.ssl.X509TrustManager)3 HttpResponse (org.apache.http.HttpResponse)3 Intent (android.content.Intent)2 SharedPreferences (android.content.SharedPreferences)2 Call (com.squareup.okhttp.Call)2 Dispatcher (com.squareup.okhttp.Dispatcher)2