當前位置: 首頁>>代碼示例>>Java>>正文


Java ResponseSource.NETWORK屬性代碼示例

本文整理匯總了Java中com.squareup.okhttp.ResponseSource.NETWORK屬性的典型用法代碼示例。如果您正苦於以下問題:Java ResponseSource.NETWORK屬性的具體用法?Java ResponseSource.NETWORK怎麽用?Java ResponseSource.NETWORK使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在com.squareup.okhttp.ResponseSource的用法示例。


在下文中一共展示了ResponseSource.NETWORK屬性的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: initResponseSource

/**
 * Initialize the source for this response. It may be corrected later if the
 * request headers forbids network use.
 */
private void initResponseSource() throws IOException {
  responseSource = ResponseSource.NETWORK;
  if (!policy.getUseCaches()) return;

  OkResponseCache responseCache = client.getOkResponseCache();
  if (responseCache == null) return;

  CacheResponse candidate = responseCache.get(
      uri, method, requestHeaders.getHeaders().toMultimap(false));
  if (candidate == null) return;

  Map<String, List<String>> responseHeadersMap = candidate.getHeaders();
  cachedResponseBody = candidate.getBody();
  if (!acceptCacheResponseType(candidate)
      || responseHeadersMap == null
      || cachedResponseBody == null) {
    Util.closeQuietly(cachedResponseBody);
    return;
  }

  RawHeaders rawResponseHeaders = RawHeaders.fromMultimap(responseHeadersMap, true);
  cachedResponseHeaders = new ResponseHeaders(uri, rawResponseHeaders);
  long now = System.currentTimeMillis();
  this.responseSource = cachedResponseHeaders.chooseResponseSource(now, requestHeaders);
  if (responseSource == ResponseSource.CACHE) {
    this.cacheResponse = candidate;
    setResponse(cachedResponseHeaders, cachedResponseBody);
  } else if (responseSource == ResponseSource.CONDITIONAL_CACHE) {
    this.cacheResponse = candidate;
  } else if (responseSource == ResponseSource.NETWORK) {
    Util.closeQuietly(cachedResponseBody);
  } else {
    throw new AssertionError();
  }
}
 
開發者ID:aabognah,項目名稱:LoRaWAN-Smart-Parking,代碼行數:39,代碼來源:HttpEngine.java

示例2: chooseResponseSource

/** Returns the source to satisfy {@code request} given this cached response. */
public ResponseSource chooseResponseSource(long nowMillis, RequestHeaders request) {
  // If this response shouldn't have been stored, it should never be used
  // as a response source. This check should be redundant as long as the
  // persistence store is well-behaved and the rules are constant.
  if (!isCacheable(request)) {
    return ResponseSource.NETWORK;
  }

  if (request.isNoCache() || request.hasConditions()) {
    return ResponseSource.NETWORK;
  }

  long ageMillis = computeAge(nowMillis);
  long freshMillis = computeFreshnessLifetime();

  if (request.getMaxAgeSeconds() != -1) {
    freshMillis = Math.min(freshMillis, TimeUnit.SECONDS.toMillis(request.getMaxAgeSeconds()));
  }

  long minFreshMillis = 0;
  if (request.getMinFreshSeconds() != -1) {
    minFreshMillis = TimeUnit.SECONDS.toMillis(request.getMinFreshSeconds());
  }

  long maxStaleMillis = 0;
  if (!mustRevalidate && request.getMaxStaleSeconds() != -1) {
    maxStaleMillis = TimeUnit.SECONDS.toMillis(request.getMaxStaleSeconds());
  }

  if (!noCache && ageMillis + minFreshMillis < freshMillis + maxStaleMillis) {
    if (ageMillis + minFreshMillis >= freshMillis) {
      headers.add("Warning", "110 HttpURLConnection \"Response is stale\"");
    }
    long oneDayMillis = 24 * 60 * 60 * 1000L;
    if (ageMillis > oneDayMillis && isFreshnessLifetimeHeuristic()) {
      headers.add("Warning", "113 HttpURLConnection \"Heuristic expiration\"");
    }
    return ResponseSource.CACHE;
  }

  if (lastModified != null) {
    request.setIfModifiedSince(lastModified);
  } else if (servedDate != null) {
    request.setIfModifiedSince(servedDate);
  }

  if (etag != null) {
    request.setIfNoneMatch(etag);
  }

  return request.hasConditions() ? ResponseSource.CONDITIONAL_CACHE : ResponseSource.NETWORK;
}
 
開發者ID:aabognah,項目名稱:LoRaWAN-Smart-Parking,代碼行數:53,代碼來源:ResponseHeaders.java


注:本文中的com.squareup.okhttp.ResponseSource.NETWORK屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。