Пример #1
0
  /**
   * 尝试从缓存获取结果, 并为请求头加入缓存控制参数.
   *
   * @return
   * @throws Throwable
   */
  @Override
  public Object loadResultFromCache() throws Throwable {
    isLoading = true;
    DiskCacheEntity cacheEntity =
        LruDiskCache.getDiskCache(params.getCacheDirName()).get(this.getCacheKey());

    if (cacheEntity != null) {
      if (HttpMethod.permitsCache(params.getMethod())) {
        Date lastModified = cacheEntity.getLastModify();
        if (lastModified.getTime() > 0) {
          params.addHeader("If-Modified-Since", toGMTString(lastModified));
        }
        String eTag = cacheEntity.getEtag();
        if (!TextUtils.isEmpty(eTag)) {
          params.addHeader("If-None-Match", eTag);
        }
      }
      return loader.loadFromCache(cacheEntity);
    } else {
      return null;
    }
  }
Пример #2
0
  /**
   * invoke via Loader
   *
   * @throws IOException
   */
  @Override
  @TargetApi(Build.VERSION_CODES.KITKAT)
  public void sendRequest() throws IOException {
    isLoading = false;

    URL url = new URL(queryUrl);
    { // init connection
      Proxy proxy = params.getProxy();
      if (proxy != null) {
        connection = (HttpURLConnection) url.openConnection(proxy);
      } else {
        connection = (HttpURLConnection) url.openConnection();
      }
      connection.setInstanceFollowRedirects(true);
      connection.setReadTimeout(params.getConnectTimeout());
      connection.setConnectTimeout(params.getConnectTimeout());
      if (connection instanceof HttpsURLConnection) {
        ((HttpsURLConnection) connection).setSSLSocketFactory(params.getSslSocketFactory());
      }
    }

    { // add headers
      try {
        Map<String, List<String>> singleMap =
            COOKIE_MANAGER.get(url.toURI(), new HashMap<String, List<String>>(0));
        List<String> cookies = singleMap.get("Cookie");
        if (cookies != null) {
          connection.setRequestProperty("Cookie", TextUtils.join(";", cookies));
        }
      } catch (Throwable ex) {
        LogUtil.e(ex.getMessage(), ex);
      }

      HashMap<String, String> headers = params.getHeaders();
      if (headers != null) {
        for (Map.Entry<String, String> entry : headers.entrySet()) {
          String name = entry.getKey();
          String value = entry.getValue();
          if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(value)) {
            connection.setRequestProperty(name, value);
          }
        }
      }
    }

    { // write body
      HttpMethod method = params.getMethod();
      connection.setRequestMethod(method.toString());
      if (HttpMethod.permitsRequestBody(method)) {
        RequestBody body = params.getRequestBody();
        if (body != null) {
          if (body instanceof ProgressBody) {
            ((ProgressBody) body).setProgressHandler(progressHandler);
          }
          String contentType = body.getContentType();
          if (!TextUtils.isEmpty(contentType)) {
            connection.setRequestProperty("Content-Type", contentType);
          }
          long contentLength = body.getContentLength();
          if (contentLength < 0) {
            connection.setChunkedStreamingMode(256 * 1024);
          } else {
            if (contentLength < Integer.MAX_VALUE) {
              connection.setFixedLengthStreamingMode((int) contentLength);
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
              connection.setFixedLengthStreamingMode(contentLength);
            } else {
              connection.setChunkedStreamingMode(256 * 1024);
            }
          }
          connection.setRequestProperty("Content-Length", String.valueOf(contentLength));
          connection.setDoOutput(true);
          body.writeTo(connection.getOutputStream());
        }
      }
    }

    LogUtil.d(queryUrl);
    int code = connection.getResponseCode();
    if (code >= 300) {
      HttpException httpException = new HttpException(code, connection.getResponseMessage());
      try {
        httpException.setResult(IOUtil.readStr(connection.getInputStream(), params.getCharset()));
      } catch (Throwable ignored) {
      }
      throw httpException;
    }

    { // save cookies
      try {
        Map<String, List<String>> headers = connection.getHeaderFields();
        if (headers != null) {
          COOKIE_MANAGER.put(url.toURI(), headers);
        }
      } catch (Throwable ex) {
        LogUtil.e(ex.getMessage(), ex);
      }
    }

    isLoading = true;
  }