use of com.squareup.okhttp.Cache in project remusic by aa112901.
the class HttpUtil method getResposeJsonObject.
public static JsonObject getResposeJsonObject(String action1, Context context, boolean forceCache) {
try {
Log.e("action-cache", action1);
File sdcache = context.getCacheDir();
//File cacheFile = new File(context.getCacheDir(), "[缓存目录]");
//30Mb
Cache cache = new Cache(sdcache.getAbsoluteFile(), 1024 * 1024 * 30);
mOkHttpClient.setCache(cache);
mOkHttpClient.setConnectTimeout(1000, TimeUnit.MINUTES);
mOkHttpClient.setReadTimeout(1000, TimeUnit.MINUTES);
Request.Builder builder = new Request.Builder().url(action1);
if (forceCache) {
builder.cacheControl(CacheControl.FORCE_CACHE);
}
Request request = builder.build();
Response response = mOkHttpClient.newCall(request).execute();
if (response.isSuccessful()) {
String c = response.body().string();
Log.e("cache", c);
JsonParser parser = new JsonParser();
JsonElement el = parser.parse(c);
return el.getAsJsonObject();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use of com.squareup.okhttp.Cache in project Rutgers-Course-Tracker by tevjef.
the class RutgersCTModule method providesOkHttpClient.
@Provides
@Singleton
public OkHttpClient providesOkHttpClient(Context context) {
OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
client.setReadTimeout(READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
client.networkInterceptors().add(new StethoInterceptor());
File httpCacheDir = new File(context.getCacheDir(), context.getString(R.string.application_name));
// 50 MiB
long httpCacheSize = 50 * 1024 * 1024;
Cache cache = new Cache(httpCacheDir, httpCacheSize);
client.setCache(cache);
if (BuildConfig.DEBUG) {
try {
cache.evictAll();
} catch (IOException e) {
e.printStackTrace();
}
}
return client;
}
Aggregations