示例#1
0
  /** Returns an exception if any, or null if the request was completed successfully. */
  public IOException getException() {
    if (mSinkException != null) {
      return mSinkException;
    }

    validateNotRecycled();

    int errorCode = nativeGetErrorCode(mUrlRequestPeer);
    switch (errorCode) {
      case UrlRequestError.SUCCESS:
        return null;
      case UrlRequestError.UNKNOWN:
        return new IOException(nativeGetErrorString(mUrlRequestPeer));
      case UrlRequestError.MALFORMED_URL:
        return new MalformedURLException("Malformed URL: " + mUrl);
      case UrlRequestError.CONNECTION_TIMED_OUT:
        return new ConnectTimeoutException("Connection timed out");
      case UrlRequestError.UNKNOWN_HOST:
        String host;
        try {
          host = new URL(mUrl).getHost();
        } catch (MalformedURLException e) {
          host = mUrl;
        }
        return new UnknownHostException("Unknown host: " + host);
      default:
        throw new IllegalStateException("Unrecognized error code: " + errorCode);
    }
  }
示例#2
0
  public void start() {
    synchronized (mLock) {
      if (mCanceled) {
        return;
      }

      validateNotStarted();
      validateNotRecycled();

      mStarted = true;

      String method = mMethod;
      if (method == null
          && ((mPostBody != null && mPostBody.length > 0) || mPostBodyChannel != null)) {
        // Default to POST if there is data to upload but no method was
        // specified.
        method = "POST";
      }

      if (method != null) {
        nativeSetMethod(mUrlRequestPeer, method);
      }

      if (mHeaders != null && !mHeaders.isEmpty()) {
        for (Entry<String, String> entry : mHeaders.entrySet()) {
          nativeAddHeader(mUrlRequestPeer, entry.getKey(), entry.getValue());
        }
      }

      if (mAdditionalHeaders != null) {
        for (Entry<String, String> entry : mAdditionalHeaders.entrySet()) {
          nativeAddHeader(mUrlRequestPeer, entry.getKey(), entry.getValue());
        }
      }

      if (mPostBody != null && mPostBody.length > 0) {
        nativeSetUploadData(mUrlRequestPeer, mPostBodyContentType, mPostBody);
      } else if (mPostBodyChannel != null) {
        nativeSetUploadChannel(
            mUrlRequestPeer, mPostBodyContentType,
            mPostBodyChannel, mUploadContentLength);
      }

      nativeStart(mUrlRequestPeer);
    }
  }