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;
}
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);
}
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;
}
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;
}
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;
}
}
Aggregations