okhttp3.Request.cacheControl()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(9.1k)|赞(0)|评价(0)|浏览(335)

本文整理了Java中okhttp3.Request.cacheControl方法的一些代码示例,展示了Request.cacheControl的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Request.cacheControl方法的具体详情如下:
包路径:okhttp3.Request
类名称:Request
方法名:cacheControl

Request.cacheControl介绍

[英]Returns the cache control directives for this response. This is never null, even if this response contains no Cache-Control header.
[中]返回此响应的缓存控制指令。即使此响应不包含缓存控制头,它也永远不会为null。

代码示例

代码示例来源:origin: square/okhttp

  1. /**
  2. * Returns a strategy to satisfy {@code request} using the a cached response {@code response}.
  3. */
  4. public CacheStrategy get() {
  5. CacheStrategy candidate = getCandidate();
  6. if (candidate.networkRequest != null && request.cacheControl().onlyIfCached()) {
  7. // We're forbidden from using the network and the cache is insufficient.
  8. return new CacheStrategy(null, null);
  9. }
  10. return candidate;
  11. }

代码示例来源:origin: com.squareup.okhttp3/okhttp

  1. /**
  2. * Returns a strategy to satisfy {@code request} using the a cached response {@code response}.
  3. */
  4. public CacheStrategy get() {
  5. CacheStrategy candidate = getCandidate();
  6. if (candidate.networkRequest != null && request.cacheControl().onlyIfCached()) {
  7. // We're forbidden from using the network and the cache is insufficient.
  8. return new CacheStrategy(null, null);
  9. }
  10. return candidate;
  11. }

代码示例来源:origin: square/picasso

  1. @Override public void onSuccess(Result result) {
  2. try {
  3. assertThat(requests.takeFirst().cacheControl().toString()).isEmpty();
  4. latch.countDown();
  5. } catch (InterruptedException e) {
  6. throw new AssertionError(e);
  7. }
  8. }

代码示例来源:origin: north2016/T-MVP

  1. @Override
  2. public Response intercept(Chain chain) throws IOException {
  3. Request request = chain.request();
  4. if (!NetWorkUtil.isNetConnected(App.getAppContext())) {
  5. request = request.newBuilder()
  6. .cacheControl(CacheControl.FORCE_CACHE)
  7. .build();
  8. Log.d("Okhttp", "no network");
  9. }
  10. Response originalResponse = chain.proceed(request);
  11. if (NetWorkUtil.isNetConnected(App.getAppContext())) {
  12. //有网的时候读接口上的@Headers里的配置,你可以在这里进行统一的设置
  13. String cacheControl = request.cacheControl().toString();
  14. return originalResponse.newBuilder()
  15. .header("Cache-Control", cacheControl)
  16. .removeHeader("Pragma")
  17. .build();
  18. } else {
  19. return originalResponse.newBuilder()
  20. .header("Cache-Control", "public, only-if-cached, max-stale=2419200")
  21. .removeHeader("Pragma")
  22. .build();
  23. }
  24. }
  25. }

代码示例来源:origin: jaydenxiao2016/AndroidFire

  1. @Override
  2. public Response intercept(Chain chain) throws IOException {
  3. Request request = chain.request();
  4. String cacheControl = request.cacheControl().toString();
  5. if (!NetWorkUtils.isNetConnected(BaseApplication.getAppContext())) {
  6. request = request.newBuilder()
  7. .cacheControl(TextUtils.isEmpty(cacheControl)?CacheControl.FORCE_NETWORK:CacheControl.FORCE_CACHE)
  8. .build();
  9. }
  10. Response originalResponse = chain.proceed(request);
  11. if (NetWorkUtils.isNetConnected(BaseApplication.getAppContext())) {
  12. //有网的时候读接口上的@Headers里的配置,你可以在这里进行统一的设置
  13. return originalResponse.newBuilder()
  14. .header("Cache-Control", cacheControl)
  15. .removeHeader("Pragma")
  16. .build();
  17. } else {
  18. return originalResponse.newBuilder()
  19. .header("Cache-Control", "public, only-if-cached, max-stale=" + CACHE_STALE_SEC)
  20. .removeHeader("Pragma")
  21. .build();
  22. }
  23. }
  24. };

代码示例来源:origin: Rukey7/MvpApp

  1. @Override
  2. public Response intercept(Chain chain) throws IOException {
  3. Request request = chain.request();
  4. if (!NetUtil.isNetworkAvailable(AndroidApplication.getContext())) {
  5. request = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
  6. Logger.e("no network");
  7. }
  8. Response originalResponse = chain.proceed(request);
  9. if (NetUtil.isNetworkAvailable(AndroidApplication.getContext())) {
  10. //有网的时候读接口上的@Headers里的配置,你可以在这里进行统一的设置
  11. String cacheControl = request.cacheControl().toString();
  12. return originalResponse.newBuilder()
  13. .header("Cache-Control", cacheControl)
  14. .removeHeader("Pragma")
  15. .build();
  16. } else {
  17. return originalResponse.newBuilder()
  18. .header("Cache-Control", "public, " + CACHE_CONTROL_CACHE)
  19. .removeHeader("Pragma")
  20. .build();
  21. }
  22. }
  23. };

代码示例来源:origin: square/okhttp

  1. return !response.cacheControl().noStore() && !request.cacheControl().noStore();

代码示例来源:origin: square/okhttp

  1. CacheControl requestCaching = request.cacheControl();
  2. if (requestCaching.noCache() || hasConditions(request)) {
  3. return new CacheStrategy(request, null);

代码示例来源:origin: com.squareup.okhttp3/okhttp

  1. return !response.cacheControl().noStore() && !request.cacheControl().noStore();

代码示例来源:origin: com.squareup.okhttp3/okhttp

  1. CacheControl requestCaching = request.cacheControl();
  2. if (requestCaching.noCache() || hasConditions(request)) {
  3. return new CacheStrategy(request, null);

代码示例来源:origin: square/picasso

  1. @Test public void withZeroRetryCountForcesLocalCacheOnly() throws Exception {
  2. responses.add(responseOf(ResponseBody.create(null, new byte[10])));
  3. Action action = TestUtils.mockAction(URI_KEY_1, URI_1);
  4. PlatformLruCache cache = new PlatformLruCache(0);
  5. BitmapHunter hunter =
  6. new BitmapHunter(picasso, dispatcher, cache, stats, action, networkHandler);
  7. hunter.retryCount = 0;
  8. hunter.hunt();
  9. assertThat(requests.takeFirst().cacheControl().toString()).isEqualTo(CacheControl.FORCE_CACHE.toString());
  10. }

代码示例来源:origin: kaku2015/ColorfulNews

  1. @Override
  2. public Response intercept(Chain chain) throws IOException {
  3. Request request = chain.request();
  4. if (!NetUtil.isNetworkAvailable()) {
  5. request = request.newBuilder()
  6. .cacheControl(CacheControl.FORCE_CACHE)
  7. .build();
  8. KLog.d("no network");
  9. }
  10. Response originalResponse = chain.proceed(request);
  11. if (NetUtil.isNetworkAvailable()) {
  12. //有网的时候读接口上的@Headers里的配置,你可以在这里进行统一的设置
  13. String cacheControl = request.cacheControl().toString();
  14. return originalResponse.newBuilder()
  15. .header("Cache-Control", cacheControl)
  16. .removeHeader("Pragma")
  17. .build();
  18. } else {
  19. return originalResponse.newBuilder()
  20. .header("Cache-Control", "public, only-if-cached, max-stale=" + CACHE_STALE_SEC)
  21. .removeHeader("Pragma")
  22. .build();
  23. }
  24. }
  25. };

代码示例来源:origin: lionoggo/FastApp

  1. @Override
  2. public Response intercept(Chain chain) throws IOException {
  3. Request request = chain.request();
  4. Response response = chain.proceed(request);
  5. String cacheControl = request.cacheControl().toString();
  6. if (TextUtils.isEmpty(cacheControl)) {
  7. cacheControl = "public,max-age=60";
  8. }
  9. return response.newBuilder().header("Cache-Control", cacheControl).build();
  10. }

代码示例来源:origin: lionoggo/Akit-Reader

  1. @Override
  2. public Response intercept(Chain chain) throws IOException {
  3. Request request = chain.request();
  4. Response response = chain.proceed(request);
  5. String cacheControl = request.cacheControl().toString();
  6. if (TextUtils.isEmpty(cacheControl)) {
  7. cacheControl = "public,max-age=60";
  8. }
  9. return response.newBuilder().header("Cache-Control", cacheControl).build();
  10. }

代码示例来源:origin: charbgr/SeismicInterceptor

  1. private Map<String, List<String>> generateBasicInfo() {
  2. Map<String, List<String>> info = new LinkedHashMap<>();
  3. info.put("URL", Arrays.asList(request.url().toString()));
  4. info.put("VERB", Arrays.asList(request.method()));
  5. if (responseExceptionWrapper.isResponse())
  6. info.put("Status", Arrays.asList(String.valueOf(responseExceptionWrapper.getResponse().code())));
  7. if (request.cacheControl() != null)
  8. info.put("Cache-Request", Arrays.asList(request.cacheControl().toString()));
  9. if (responseExceptionWrapper.isResponse() && responseExceptionWrapper.getResponse().cacheControl() != null)
  10. info.put("Cache-Response", Arrays.asList(responseExceptionWrapper.getResponse().cacheControl().toString()));
  11. return info;
  12. }

代码示例来源:origin: duzechao/OKHttpUtils

  1. /**
  2. * Returns a strategy to satisfy {@code request} using the a cached response {@code response}.
  3. */
  4. public CacheStrategy get() {
  5. CacheStrategy candidate = getCandidate();
  6. if (candidate.networkRequest != null && request.cacheControl().onlyIfCached()) {
  7. // We're forbidden from using the network and the cache is insufficient.
  8. return new CacheStrategy(null, null);
  9. }
  10. return candidate;
  11. }

代码示例来源:origin: com.github.ljun20160606/okhttp

  1. /**
  2. * Returns a strategy to satisfy {@code request} using the a cached response {@code response}.
  3. */
  4. public CacheStrategy get() {
  5. CacheStrategy candidate = getCandidate();
  6. if (candidate.networkRequest != null && request.cacheControl().onlyIfCached()) {
  7. // We're forbidden from using the network and the cache is insufficient.
  8. return new CacheStrategy(null, null);
  9. }
  10. return candidate;
  11. }

代码示例来源:origin: huxq17/SwipeCardsView

  1. /**
  2. * Returns a strategy to satisfy {@code request} using the a cached response
  3. * {@code response}.
  4. */
  5. public CacheStrategy get() {
  6. CacheStrategy candidate = getCandidate();
  7. if (candidate.networkRequest != null && request.cacheControl().onlyIfCached()) {
  8. // We're forbidden from using the network and the cache is insufficient.
  9. return new CacheStrategy(null, null);
  10. }
  11. return candidate;
  12. }

代码示例来源:origin: huxq17/tractor

  1. /**
  2. * Returns a strategy to satisfy {@code request} using the a cached response
  3. * {@code response}.
  4. */
  5. public CacheStrategy get() {
  6. CacheStrategy candidate = getCandidate();
  7. if (candidate.networkRequest != null && request.cacheControl().onlyIfCached()) {
  8. // We're forbidden from using the network and the cache is insufficient.
  9. return new CacheStrategy(null, null);
  10. }
  11. return candidate;
  12. }

代码示例来源:origin: apache/servicemix-bundles

  1. /**
  2. * Returns a strategy to satisfy {@code request} using the a cached response {@code response}.
  3. */
  4. public CacheStrategy get() {
  5. CacheStrategy candidate = getCandidate();
  6. if (candidate.networkRequest != null && request.cacheControl().onlyIfCached()) {
  7. // We're forbidden from using the network and the cache is insufficient.
  8. return new CacheStrategy(null, null);
  9. }
  10. return candidate;
  11. }

相关文章