本文整理匯總了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;
}