Java Code Examples for com.squareup.okhttp.RequestBody#writeTo()

The following examples show how to use com.squareup.okhttp.RequestBody#writeTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SendFeedBack.java    From xDrip with GNU General Public License v3.0 6 votes vote down vote up
/**
 * https://github.com/square/okhttp/issues/350
 */
private RequestBody forceContentLength(final RequestBody requestBody) throws IOException {
    final Buffer buffer = new Buffer();
    requestBody.writeTo(buffer);
    return new RequestBody() {
        @Override
        public MediaType contentType() {
            return requestBody.contentType();
        }

        @Override
        public long contentLength() {
            return buffer.size();
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            sink.write(buffer.snapshot());
        }
    };
}
 
Example 2
Source File: SendFeedBack.java    From xDrip with GNU General Public License v3.0 6 votes vote down vote up
private RequestBody gzip(final RequestBody body) {
    return new RequestBody() {
        @Override
        public MediaType contentType() {
            return body.contentType();
        }

        @Override
        public long contentLength() {
            return -1; // We don't know the compressed length in advance!
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
            body.writeTo(gzipSink);
            gzipSink.close();
        }
    };
}
 
Example 3
Source File: SendFeedBack.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
/**
 * https://github.com/square/okhttp/issues/350
 */
private RequestBody forceContentLength(final RequestBody requestBody) throws IOException {
    final Buffer buffer = new Buffer();
    requestBody.writeTo(buffer);
    return new RequestBody() {
        @Override
        public MediaType contentType() {
            return requestBody.contentType();
        }

        @Override
        public long contentLength() {
            return buffer.size();
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            sink.write(buffer.snapshot());
        }
    };
}
 
Example 4
Source File: SendFeedBack.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
private RequestBody gzip(final RequestBody body) {
    return new RequestBody() {
        @Override
        public MediaType contentType() {
            return body.contentType();
        }

        @Override
        public long contentLength() {
            return -1; // We don't know the compressed length in advance!
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
            body.writeTo(gzipSink);
            gzipSink.close();
        }
    };
}
 
Example 5
Source File: OkHttpLogInterceptor.java    From wasp with Apache License 2.0 5 votes vote down vote up
@Override
public Response intercept(Chain chain) throws IOException {
  Request request = chain.request();
  Logger.d("---> REQUEST " + request.method() + " " + request.urlString());
  logHeaders(request.headers());
  //copy original request for logging request body
  Request copy = request.newBuilder().build();
  RequestBody requestBody = copy.body();
  if (requestBody == null) {
    Logger.d("Body - no body");
  } else {
    Buffer buffer = new Buffer();
    requestBody.writeTo(buffer);
    Logger.d("Body - " + buffer.readString(requestBody.contentType().charset()));
  }
  Logger.d("---> END");

  long t1 = System.nanoTime();
  Response response = chain.proceed(request);
  long t2 = System.nanoTime();

  Logger.d("<--- RESPONSE " + response.code() + " " + response.request().urlString());
  logHeaders(response.headers());
  String responseBody = response.body().string();
  Logger.d(TextUtils.isEmpty(responseBody) ? "Body - no body" : "Body - " + responseBody);
  Logger.d("<--- END " + "(Size: " + response.body().contentLength() + " bytes - "
      + "Network time: " + (t2 - t1) / MILLI_AS_NANO + " ms)");

  return response;
}
 
Example 6
Source File: AllureOkHttp.java    From allure-java with Apache License 2.0 4 votes vote down vote up
private static String readRequestBody(final RequestBody requestBody) throws IOException {
    final Buffer buffer = new Buffer();
    requestBody.writeTo(buffer);
    return buffer.readString(StandardCharsets.UTF_8);
}