本文整理匯總了Java中com.squareup.okhttp.OkHttpClient.setCache方法的典型用法代碼示例。如果您正苦於以下問題:Java OkHttpClient.setCache方法的具體用法?Java OkHttpClient.setCache怎麽用?Java OkHttpClient.setCache使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.squareup.okhttp.OkHttpClient
的用法示例。
在下文中一共展示了OkHttpClient.setCache方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createOkHttpClient
import com.squareup.okhttp.OkHttpClient; //導入方法依賴的package包/類
private OkHttpClient createOkHttpClient() {
OkHttpClient okHttpClient = new OkHttpClient();
// if we have response cache let's use it!
if (responseCache != null) {
okHttpClient.setCache(responseCache);
}
// add auth interceptors
switch (authType) {
case OAUTH:
if (oAuthConsumer == null) {
throw new IllegalStateException("a RetrofitHttpOAuthConsumer must be created before creating OKClient");
}
okHttpClient.interceptors().add(new OAuthSigningInterceptor(oAuthConsumer));
break;
case API_TOKEN:
okHttpClient.interceptors().add(new ApiTokenSigningInterceptor(apiToken));
break;
default:
throw new IllegalStateException("AuthType " + authType + " isn't supported.");
}
// add all other application interceptors
if (applicationInterceptors != null && !applicationInterceptors.isEmpty()) {
okHttpClient.interceptors().addAll(applicationInterceptors);
}
// add all other network interceptors
if (networkInterceptors != null && !networkInterceptors.isEmpty()) {
okHttpClient.networkInterceptors().addAll(networkInterceptors);
}
return okHttpClient;
}
示例2: createStatic
import com.squareup.okhttp.OkHttpClient; //導入方法依賴的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);
}
示例3: okHttp2Client
import com.squareup.okhttp.OkHttpClient; //導入方法依賴的package包/類
@Bean
@ConditionalOnMissingBean
public OkHttpClient okHttp2Client() throws IOException {
OkHttpClient okHttpClient = new OkHttpClient();
if (properties.getCache().getMode() != OkHttpProperties.Cache.Mode.NONE) {
okHttpClient.setCache(okHttp2Cache());
}
if (cookieHandler != null) {
okHttpClient.setCookieHandler(cookieHandler);
}
OkHttpProperties.Timeout connectTimeout = properties.getConnectTimeout();
if (connectTimeout != null) {
okHttpClient.setConnectTimeout(connectTimeout.getValue(), connectTimeout.getUnit());
}
OkHttpProperties.Timeout readTimeout = properties.getReadTimeout();
if (readTimeout != null) {
okHttpClient.setReadTimeout(readTimeout.getValue(), readTimeout.getUnit());
}
OkHttpProperties.Timeout writeTimeout = properties.getWriteTimeout();
if (writeTimeout != null) {
okHttpClient.setWriteTimeout(writeTimeout.getValue(), writeTimeout.getUnit());
}
if (dns != null) {
okHttpClient.setDns(dns);
}
okHttpClient.setFollowRedirects(properties.isFollowRedirects());
okHttpClient.setFollowSslRedirects(properties.isFollowSslRedirects());
okHttpClient.setRetryOnConnectionFailure(properties.isRetryOnConnectionFailure());
if (applicationInterceptors != null && !applicationInterceptors.isEmpty()) {
okHttpClient.interceptors().addAll(applicationInterceptors);
}
if (networkInterceptors != null && !networkInterceptors.isEmpty()) {
okHttpClient.networkInterceptors().addAll(networkInterceptors);
}
if (configurers != null) {
for (Configurer<OkHttpClient> configurer : configurers) {
configurer.configure(okHttpClient);
}
}
return okHttpClient;
}