Пример #1
0
    @Override
    protected Document doInBackground(String... params) {
      Document document = null;
      try {
        LogUtil.e(params[0]);
        document = Jsoup.connect(params[0]).timeout(9000).post();
        //                Elements elements = document.getElementsByClass("ulTextlist_2 clear");

      } catch (IOException e) {
        e.printStackTrace();
      }
      if (document != null) {
        return document;
      }
      return null;
    }
Пример #2
0
  @Override
  public long getExpiration() {
    if (connection == null) return -1L;

    long expiration = -1L;

    // from max-age
    String cacheControl = connection.getHeaderField("Cache-Control");
    if (!TextUtils.isEmpty(cacheControl)) {
      StringTokenizer tok = new StringTokenizer(cacheControl, ",");
      while (tok.hasMoreTokens()) {
        String token = tok.nextToken().trim().toLowerCase();
        if (token.startsWith("max-age")) {
          int eqIdx = token.indexOf('=');
          if (eqIdx > 0) {
            try {
              String value = token.substring(eqIdx + 1).trim();
              long seconds = Long.parseLong(value);
              if (seconds > 0L) {
                expiration = System.currentTimeMillis() + seconds * 1000L;
              }
            } catch (Throwable ex) {
              LogUtil.e(ex.getMessage(), ex);
            }
          }
          break;
        }
      }
    }

    // from expires
    if (expiration <= 0L) {
      expiration = connection.getExpiration();
    }

    if (expiration <= 0) {
      expiration = Long.MAX_VALUE;
    }
    return expiration;
  }
Пример #3
0
 @Override
 public long getContentLength() {
   long result = 0;
   if (connection != null) {
     try {
       result = connection.getContentLength();
     } catch (Throwable ex) {
       LogUtil.e(ex.getMessage(), ex);
     }
     if (result < 1) {
       try {
         result = this.getInputStream().available();
       } catch (Throwable ignored) {
       }
     }
   } else {
     try {
       result = this.getInputStream().available();
     } catch (Throwable ignored) {
     }
   }
   return result;
 }
Пример #4
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;
  }