@SuppressWarnings("deprecation")
  private static void setConnectionParametersForRequest(
      com.squareup.okhttp.Request.Builder builder, Request<?> request)
      throws IOException, AuthFailureError {
    switch (request.getMethod()) {
      case Request.Method.DEPRECATED_GET_OR_POST:
        // Ensure backwards compatibility.
        // Volley assumes a request with a null body is a GET.
        byte[] postBody = request.getPostBody();

        if (postBody != null) {
          builder.post(
              RequestBody.create(MediaType.parse(request.getPostBodyContentType()), postBody));
        }
        break;

      case Request.Method.GET:
        builder.get();
        break;

      case Request.Method.DELETE:
        builder.delete();
        break;

      case Request.Method.POST:
        builder.post(createRequestBody(request));
        break;

      case Request.Method.PUT:
        builder.put(createRequestBody(request));
        break;

      case Request.Method.HEAD:
        builder.head();
        break;

      case Request.Method.OPTIONS:
        builder.method("OPTIONS", null);
        break;

      case Request.Method.TRACE:
        builder.method("TRACE", null);
        break;

      case Request.Method.PATCH:
        builder.patch(createRequestBody(request));
        break;

      default:
        throw new IllegalStateException("Unknown method type.");
    }
  }
Ejemplo n.º 2
0
  public static @NonNull SmashNetworkData perform(SmashRequest<?> request) throws SmashError {
    SmashNetworkData data = new SmashNetworkData();

    try {
      Request.Builder okBuilder = new Request.Builder().url(request.getUrl());
      okBuilder.removeHeader("User-Agent").addHeader("User-Agent", Smash.getUserAgent());

      BufferedSource body = getBody(request);
      switch (request.getMethod()) {
        case GET:
          okBuilder = okBuilder.get();
          break;
        case POST:
          okBuilder = okBuilder.post(convertBody(request, body));
          break;
        case PUT:
          okBuilder = okBuilder.put(convertBody(request, body));
          break;
        case DELETE:
          okBuilder = okBuilder.delete(convertBody(request, body));
          break;
        case HEAD:
          okBuilder = okBuilder.head();
          break;
        case PATCH:
          okBuilder = okBuilder.patch(convertBody(request, body));
          break;
      }

      Request okRequest = okBuilder.build();
      Response okResponse = sHttpClient.newCall(okRequest).execute();

      if (body != null) {
        body.close();
      }

      data.code = okResponse.code();
      data.headers = okResponse.headers();
      data.source = okResponse.body().source();
      data.length = okResponse.body().contentLength();
    } catch (IOException ioe) {
      throw new SmashError(ioe);
    }

    return data;
  }