예제 #1
0
  /**
   * Attempts to prepare the request for a retry. If there are no more attempts remaining in the
   * request's retry policy, a timeout exception is thrown.
   *
   * @param request The request to use.
   */
  private static void attemptRetryOnException(
      String logPrefix, Request<?> request, VolleyError exception) throws VolleyError {
    RetryPolicy retryPolicy = request.getRetryPolicy();
    int oldTimeout = request.getTimeoutMs();

    try {
      retryPolicy.retry(exception);
    } catch (VolleyError e) {
      request.addMarker(String.format("%s-timeout-giveup [timeout=%s]", logPrefix, oldTimeout));
      throw e;
    }
    request.addMarker(String.format("%s-retry [timeout=%s]", logPrefix, oldTimeout));
  }
  @Override
  public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
      throws IOException, AuthFailureError {
    OkHttpClient client = okHttpClient.clone();
    int timeoutMs = request.getTimeoutMs();
    client.setConnectTimeout(timeoutMs, TimeUnit.MILLISECONDS);
    client.setReadTimeout(timeoutMs, TimeUnit.MILLISECONDS);
    client.setWriteTimeout(timeoutMs, TimeUnit.MILLISECONDS);

    com.squareup.okhttp.Request.Builder okHttpRequestBuilder =
        new com.squareup.okhttp.Request.Builder();
    okHttpRequestBuilder.url(request.getUrl());

    Map<String, String> headers = request.getHeaders();

    for (final String name : headers.keySet()) {
      okHttpRequestBuilder.addHeader(name, headers.get(name));
    }

    for (final String name : additionalHeaders.keySet()) {
      okHttpRequestBuilder.addHeader(name, additionalHeaders.get(name));
    }

    setConnectionParametersForRequest(okHttpRequestBuilder, request);

    com.squareup.okhttp.Request okHttpRequest = okHttpRequestBuilder.build();
    Call okHttpCall = client.newCall(okHttpRequest);
    Response okHttpResponse = okHttpCall.execute();

    StatusLine responseStatus =
        new BasicStatusLine(
            parseProtocol(okHttpResponse.protocol()),
            okHttpResponse.code(),
            okHttpResponse.message());

    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    response.setEntity(entityFromOkHttpResponse(okHttpResponse));

    Headers responseHeaders = okHttpResponse.headers();

    for (int i = 0, len = responseHeaders.size(); i < len; i++) {
      final String name = responseHeaders.name(i), value = responseHeaders.value(i);

      if (name != null) {
        response.addHeader(new BasicHeader(name, value));
      }
    }

    return response;
  }
예제 #3
0
  /**
   * Opens an {@link HttpURLConnection} with parameters.
   *
   * @param url
   * @return an open connection
   * @throws IOException
   */
  private HttpURLConnection openConnection(URL url, Request<?> request) throws IOException {
    HttpURLConnection connection = createConnection(url);

    int timeoutMs = request.getTimeoutMs();
    connection.setConnectTimeout(timeoutMs);
    connection.setReadTimeout(timeoutMs);
    connection.setUseCaches(false);
    connection.setDoInput(true);

    // use caller-provided custom SslSocketFactory, if any, for HTTPS
    if ("https".equals(url.getProtocol()) && mSslSocketFactory != null) {
      ((HttpsURLConnection) connection).setSSLSocketFactory(mSslSocketFactory);
    }

    return connection;
  }
예제 #4
0
  public HttpResponse performMultiPartRequest(
      Request<?> request, Map<String, String> additionalHeaders)
      throws IOException, AuthFailureError {
    HttpUriRequest httpRequest = createMultiPartRequest(request, additionalHeaders);
    addHeaders(httpRequest, additionalHeaders);
    addHeaders(httpRequest, request.getHeaders());
    HttpParams httpParams = httpRequest.getParams();
    int timeoutMs = request.getTimeoutMs();
    // TODO: Reevaluate this connection timeout based on more wide-scale
    // data collection and possibly different for wifi vs. 3G.
    HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
    HttpConnectionParams.setSoTimeout(httpParams, timeoutMs);

    /* Make a thread safe connection manager for the client */
    HttpClient httpClient = new DefaultHttpClient(httpParams);

    return httpClient.execute(httpRequest);
  }
예제 #5
0
  public HttpResponse performMultiPartRequest(
      Request<?> request, Map<String, String> additionalHeaders)
      throws IOException, AuthFailureError {
    HttpUriRequest httpRequest = createMultiPartRequest(request, additionalHeaders);
    addHeaders(httpRequest, additionalHeaders);
    addHeaders(httpRequest, request.getHeaders());
    HttpParams httpParams = httpRequest.getParams();
    int timeoutMs = request.getTimeoutMs();

    if (timeoutMs != -1) {
      HttpConnectionParams.setSoTimeout(httpParams, timeoutMs);
    }

    /* Make a thread safe connection manager for the client */
    HttpClient httpClient = new DefaultHttpClient(httpParams);

    return httpClient.execute(httpRequest);
  }