本文整理汇总了Java中com.squareup.okhttp.Cache类的典型用法代码示例。如果您正苦于以下问题:Java Cache类的具体用法?Java Cache怎么用?Java Cache使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Cache类属于com.squareup.okhttp包,在下文中一共展示了Cache类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initClient
import com.squareup.okhttp.Cache; //导入依赖的package包/类
@Override
protected LiteClient initClient(ClientSettings settings) {
if(client==null) client = new OkHttpClient();
client.setProxy(settings.getProxy()).setProxySelector(settings.getProxySelector()).setSocketFactory(settings.getSocketFactory())
.setSslSocketFactory(settings.getSslSocketFactory())
.setHostnameVerifier(settings.getHostnameVerifier()).setFollowSslRedirects(settings.isFollowSslRedirects())
.setFollowRedirects(settings.isFollowRedirects());
client.setRetryOnConnectionFailure(settings.getMaxRetryCount()>0);
client.setConnectTimeout(settings.getConnectTimeout(), TimeUnit.MILLISECONDS);
client.setReadTimeout(settings.getReadTimeout(),TimeUnit.MILLISECONDS);
client.setWriteTimeout(settings.getWriteTimeout(), TimeUnit.MILLISECONDS);
client.setCookieHandler(cookieHandler);
if(settings.getCacheDir()!=null){
client.setCache(new Cache(settings.getCacheDir(),settings.getCacheMaxSize()));
}
return new Ok2Lite(client);
}
示例2: initGithub
import com.squareup.okhttp.Cache; //导入依赖的package包/类
public GitHub initGithub() {
String tmpDirPath = System.getProperty("java.io.tmpdir");
File cacheDirectoryParent = new File(tmpDirPath);
File cacheDirectory = new File(cacheDirectoryParent, "okhttpCache");
if (!cacheDirectory.exists()) {
cacheDirectory.mkdir();
}
Cache cache = new Cache(cacheDirectory, 100 * 1024 * 1024);
try {
return GitHubBuilder.fromCredentials()
.withRateLimitHandler(RateLimitHandler.WAIT)
.withAbuseLimitHandler(AbuseLimitHandler.WAIT)
.withConnector(new OkHttpConnector(new OkUrlFactory(new OkHttpClient().setCache(cache))))
.build();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
示例3: create
import com.squareup.okhttp.Cache; //导入依赖的package包/类
public static <T> T create(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);
}
示例4: provideOkHttpClient
import com.squareup.okhttp.Cache; //导入依赖的package包/类
@Provides
@Singleton
OkHttpClient provideOkHttpClient(Context context) {
OkHttpClient client = new OkHttpClient();
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
client.setCookieHandler(cookieManager);
try {
Cache cache = new Cache(context.getCacheDir(), 10 * 1024 * 1024); // 10 MiB
client.setCache(cache);
} catch (IOException e) {
Toast.makeText(context, "Cannot create cache for http responses", Toast.LENGTH_SHORT).show();
}
return client;
}
示例5: setPicasso
import com.squareup.okhttp.Cache; //导入依赖的package包/类
public void setPicasso() {
OkHttpClient client = new OkHttpClient();
client.networkInterceptors().add(new StethoInterceptor());
File cache = new File(this.getCacheDir(), PICASSO_CACHE);
if (!cache.exists()) {
//noinspection ResultOfMethodCallIgnored
cache.mkdirs();
}
try {
client.setCache(new Cache(cache, PICASSO_CACHE_SIZE));
} catch (IOException e) {
e.printStackTrace();
}
Picasso picasso = new Picasso.Builder(this)
.downloader(new OkHttpDownloader(client))
.build();
Picasso.setSingletonInstance(picasso);
}
示例6: init
import com.squareup.okhttp.Cache; //导入依赖的package包/类
public static void init(AppStructureConfig.HttpConfig config){
if(config == null){
throw new IllegalArgumentException("OkHttpComponent HttpConfig initialization parameters can not be NULL!");
}
if(mOkHttpClient == null){
synchronized (OkHttpComponent.class){
if(mOkHttpClient == null){
File cacheDir = new File(AppStructure.getInstance().getCacheDir(), config.getCacheDir());
mOkHttpClient = new OkHttpClient();
mOkHttpClient.setCache(new Cache(cacheDir, config.getCacheSize()));
mOkHttpClient.setConnectTimeout(config.getConnectTimeout(), TimeUnit.MILLISECONDS);
mOkHttpClient.setReadTimeout(config.getReadTimeout(), TimeUnit.MILLISECONDS);
if(AppStructure.isDebug()){
TLog.i(LOG_TAG,"OkHttpComponent initialization!");
}
}
}
}
}
示例7: DefaultNetworkRequestProvider
import com.squareup.okhttp.Cache; //导入依赖的package包/类
@Inject
public DefaultNetworkRequestProvider(@NonNull Context applicationContext,
@NonNull AppStringsProvider appStringsProvider) {
mOkHttpClient = new OkHttpClient();
mOkHttpClient.setConnectTimeout(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS);
mOkHttpClient.setReadTimeout(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS);
mOkHttpClient.setWriteTimeout(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS);
// Configure a response cache for OkHttp so we get some free bandwidth / battery preservation.
File responseCacheDirectory = new File(applicationContext.getCacheDir(), RESPONSE_CACHE_DIRECTORY);
mOkHttpClient.setCache(new Cache(responseCacheDirectory, RESPONSE_CACHE_SIZE_MEGABYTES));
mConnectivityManager = (ConnectivityManager) applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
mApiKey = appStringsProvider.getString(R.string.football_api_key);
}
开发者ID:MarcelBraghetto,项目名称:AndroidNanoDegreeProject3,代码行数:18,代码来源:DefaultNetworkRequestProvider.java
示例8: instance
import com.squareup.okhttp.Cache; //导入依赖的package包/类
public static BibleGatewayService instance(@NonNull Context context) {
if (sService == null) {
final Cache cache = new Cache(new File(context.getCacheDir(), HTTP_CACHE_FILE_NAME),
HTTP_CACHE_SIZE);
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
final OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setConnectTimeout(30, TimeUnit.SECONDS);
okHttpClient.setReadTimeout(30, TimeUnit.SECONDS);
okHttpClient.setCache(cache);
okHttpClient.interceptors().add(interceptor);
Retrofit retrofit = new Retrofit.Builder().
baseUrl(BibleGatewayService.API_BASE_URL).
addConverterFactory(GsonConverterFactory.create()).
addCallAdapterFactory(RxJavaCallAdapterFactory.create()).
client(okHttpClient).
build();
sService = retrofit.create(BibleGatewayService.class);
}
return sService;
}
示例9: providesOkHttpClient
import com.squareup.okhttp.Cache; //导入依赖的package包/类
@Provides
@Singleton
public OkHttpClient providesOkHttpClient(Context context) {
OkHttpClient client = new OkHttpClient();
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
client.interceptors().add(interceptor);
client.networkInterceptors().add(new StethoInterceptor());
File httpCacheDir = new File(context.getCacheDir() + "/okhttp");
httpCacheDir.mkdirs();
cleanDir(httpCacheDir);
long httpCacheSize = 50 * 1024 * 1024; // 50 MiB
Cache cache = new Cache(httpCacheDir, httpCacheSize);
client.setCache(cache);
return client;
}
示例10: createOkHttpClient
import com.squareup.okhttp.Cache; //导入依赖的package包/类
static OkHttpClient createOkHttpClient(final Context context) {
OkHttpClient client = new OkHttpClient();
// Install an HTTP cache in the application cache directory.
try {
final File cacheDir = new File(context.getCacheDir(), "http");
final Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
client.setCache(cache);
}
catch (IOException e) {
//Timber.e(e, "Unable to install disk cache.");
}
return client;
}
示例11: provideRestAdapter
import com.squareup.okhttp.Cache; //导入依赖的package包/类
@Provides
public RestAdapter provideRestAdapter() {
int SIZE_OF_CACHE = 1024;
OkHttpClient ok = new OkHttpClient();
ok.setReadTimeout(30, TimeUnit.SECONDS);
ok.setConnectTimeout(30, TimeUnit.SECONDS);
try {
Cache responseCache = new Cache(DevConApplication.getInstance().getCacheDir(), SIZE_OF_CACHE);
ok.setCache(responseCache);
} catch (Exception e) {
Log.d("OkHttp", "Unable to set http cache", e);
}
Executor executor = Executors.newCachedThreadPool();
return new RestAdapter.Builder()
.setExecutors(executor, executor)
.setClient(new OkClient(ok))
.setEndpoint(DevConApplication.API_ENDPOINT)
.setRequestInterceptor(new ApiRequestInterceptor())
// .setLogLevel(RestAdapter.LogLevel.FULL).setLog(new AndroidLog("RETROFIT"))
.build();
}
示例12: provideRestAdapter
import com.squareup.okhttp.Cache; //导入依赖的package包/类
@Provides
public RestAdapter provideRestAdapter(MockWebServer mockWebServer) {
int SIZE_OF_CACHE = 1024;
OkHttpClient ok = new OkHttpClient();
ok.setReadTimeout(30, TimeUnit.SECONDS);
ok.setConnectTimeout(30, TimeUnit.SECONDS);
try {
Cache responseCache = new Cache(DevConApplication.getInstance().getCacheDir(), SIZE_OF_CACHE);
ok.setCache(responseCache);
} catch (Exception e) {
Log.d("OkHttp", "Unable to set http cache", e);
}
Executor executor = Executors.newCachedThreadPool();
return new RestAdapter.Builder()
.setExecutors(executor, executor)
.setClient(new OkClient(ok))
.setEndpoint(mockWebServer.getUrl("/").toString())
.setRequestInterceptor(new ApiRequestInterceptor())
.build();
}
示例13: createStatic
import com.squareup.okhttp.Cache; //导入依赖的package包/类
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() {
PreferencesUtility prefs = PreferencesUtility.getInstance(context);
@Override
public void intercept(RequestFacade request) {
//7-days cache
request.addHeader("Cache-Control", String.format("max-age=%d,%smax-stale=%d", Integer.valueOf(60 * 60 * 24 * 7), prefs.loadArtistImages() ? "" : "only-if-cached,", 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);
}
示例14: okHttp2Cache
import com.squareup.okhttp.Cache; //导入依赖的package包/类
@Lazy
@Bean
@ConditionalOnMissingBean
public Cache okHttp2Cache() throws IOException {
File cacheDir = getCacheDir("okhttp2-cache");
return new Cache(cacheDir, properties.getCache().getSize());
}
示例15: providesOkHttpClient
import com.squareup.okhttp.Cache; //导入依赖的package包/类
@Provides
@Singleton
public OkHttpClient providesOkHttpClient(Application application) {
OkHttpClient client = new OkHttpClient();
client.setCache(new Cache(application.getCacheDir(), CACHE_SIZE));
HttpLoggingInterceptor logger = new HttpLoggingInterceptor();
logger.setLevel(BuildConfig.DEBUG ? HttpLoggingInterceptor.Level.HEADERS : HttpLoggingInterceptor.Level.NONE);
client.interceptors().add(logger);
return client;
}