コード例 #1
0
ファイル: BasicNetwork.java プロジェクト: Guruev/DatingApp
  /** Reads the contents of HttpEntity into a byte[]. */
  private byte[] entityToBytes(HttpEntity entity) throws IOException, ServerError {
    PoolingByteArrayOutputStream bytes =
        new PoolingByteArrayOutputStream(mPool, (int) entity.getContentLength());
    byte[] buffer = null;
    try {
      InputStream in = entity.getContent();
      Header contentEncoding = entity.getContentEncoding();
      if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
        in = new GZIPInputStream(in);
      }

      if (in == null) {
        throw new ServerError();
      }
      buffer = mPool.getBuf(1024);
      int count;
      while ((count = in.read(buffer)) != -1) {
        bytes.write(buffer, 0, count);
      }
      return bytes.toByteArray();
    } finally {
      try {
        // Close the InputStream and release the resources by "consuming the content".
        entity.consumeContent();
      } catch (IOException e) {
        // This can happen if there was an exception above that left the entity in
        // an invalid state.
        VolleyLog.v("Error occured when calling consumingContent");
      }
      mPool.returnBuf(buffer);
      bytes.close();
    }
  }
コード例 #2
0
ファイル: BasicNetwork.java プロジェクト: Guruev/DatingApp
 /** Logs requests that took over SLOW_REQUEST_THRESHOLD_MS to complete. */
 private void logSlowRequests(
     long requestLifetime, Request<?> request, byte[] responseContents, StatusLine statusLine) {
   if (DEBUG || requestLifetime > SLOW_REQUEST_THRESHOLD_MS) {
     VolleyLog.d(
         "HTTP response for request=<%s> [lifetime=%d], [size=%s], " + "[rc=%d], [retryCount=%s]",
         request,
         requestLifetime,
         responseContents != null ? responseContents.length : "null",
         statusLine.getStatusCode(),
         request.getRetryPolicy().getCurrentRetryCount());
   }
 }
コード例 #3
0
ファイル: BasicNetwork.java プロジェクト: Guruev/DatingApp
  @Override
  public NetworkResponse performRequest(Request<?> request) throws VolleyError {
    long requestStart = SystemClock.elapsedRealtime();
    while (true) {
      HttpResponse httpResponse = null;
      byte[] responseContents = null;
      Map<String, String> responseHeaders = new HashMap<String, String>();
      try {
        // Gather headers.
        Map<String, String> headers = new HashMap<String, String>();
        addCacheHeaders(headers, request.getCacheEntry());
        httpResponse = mHttpStack.performRequest(request, headers);

        StatusLine statusLine = httpResponse.getStatusLine();
        int statusCode = statusLine.getStatusCode();

        responseHeaders = convertHeaders(httpResponse.getAllHeaders());
        // Handle cache validation.
        if (statusCode == HttpStatus.SC_NOT_MODIFIED) {
          return new NetworkResponse(
              HttpStatus.SC_NOT_MODIFIED, request.getCacheEntry().data, responseHeaders, true);
        }

        // Some responses such as 204s do not have content.  We must check.
        if (httpResponse.getEntity() != null) {
          responseContents = entityToBytes(httpResponse.getEntity());
        } else {
          // Add 0 byte response as a way of honestly representing a
          // no-content request.
          responseContents = new byte[0];
        }

        // if the request is slow, log it.
        long requestLifetime = SystemClock.elapsedRealtime() - requestStart;
        logSlowRequests(requestLifetime, request, responseContents, statusLine);

        if (statusCode < 200 || statusCode > 299) {
          throw new IOException();
        }
        return new NetworkResponse(statusCode, responseContents, responseHeaders, false);
      } catch (SocketTimeoutException e) {
        attemptRetryOnException("socket", request, new TimeoutError());
      } catch (ConnectTimeoutException e) {
        attemptRetryOnException("connection", request, new TimeoutError());
      } catch (MalformedURLException e) {
        throw new RuntimeException("Bad URL " + request.getUrl(), e);
      } catch (IOException e) {
        int statusCode = 0;
        NetworkResponse networkResponse = null;
        if (httpResponse != null) {
          statusCode = httpResponse.getStatusLine().getStatusCode();
        } else {
          throw new NoConnectionError(e);
        }
        VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl());
        if (responseContents != null) {
          networkResponse =
              new NetworkResponse(statusCode, responseContents, responseHeaders, false);
          if (statusCode == HttpStatus.SC_UNAUTHORIZED || statusCode == HttpStatus.SC_FORBIDDEN) {
            attemptRetryOnException("auth", request, new AuthFailureError(networkResponse));
          } else {
            // TODO: Only throw ServerError for 5xx status codes.
            throw new ServerError(networkResponse);
          }
        } else {
          throw new NetworkError(networkResponse);
        }
      }
    }
  }
コード例 #4
0
ファイル: BasicNetwork.java プロジェクト: Guruev/DatingApp
 protected void logError(String what, String url, long start) {
   long now = SystemClock.elapsedRealtime();
   VolleyLog.v("HTTP ERROR(%s) %d ms to fetch %s", what, (now - start), url);
 }