@Override public MediaType contentType() { return delegate.contentType(); }
@Override public MediaType contentType() { return responseBody.contentType(); }
@Override public MediaType contentType() { return mBody.contentType(); }
@Override public MediaType contentType() { return(wrapped.contentType()); }
@Override public MediaType contentType() { return(wrapped.contentType()); }
@Override public MediaType contentType() { return mResponseBody.contentType(); }
@Override public MediaType contentType() { return mResponseBody.contentType(); }
@Override public MediaType contentType() { return delegate.contentType(); }
@Override public MediaType contentType() { return responseBody.contentType(); }
@Override public MediaType contentType() { return mDelegate.contentType(); }
@Override public MediaType contentType() { return responseBody.contentType(); }
private Charset charset() { MediaType contentType = contentType(); return contentType != null ? contentType.charset(UTF_8) : UTF_8; }
static ResponseBody buffer(final ResponseBody body) throws IOException { Buffer buffer = new Buffer(); body.source().readAll(buffer); return ResponseBody.create(body.contentType(), body.contentLength(), buffer); }
private Charset charset() { MediaType contentType = contentType(); return contentType != null ? contentType.charset(UTF_8) : UTF_8; }
public static <T> JsonResponse<T> execute(JsonCodec<T> codec, OkHttpClient client, Request request) { try (Response response = client.newCall(request).execute()) { // TODO: fix in OkHttp: https://github.com/square/okhttp/issues/3111 if ((response.code() == 307) || (response.code() == 308)) { String location = response.header(LOCATION); if (location != null) { request = request.newBuilder().url(location).build(); return execute(codec, client, request); } } ResponseBody responseBody = requireNonNull(response.body()); String body = responseBody.string(); if (isJson(responseBody.contentType())) { return new JsonResponse<>(response.code(), response.message(), response.headers(), body, codec); } return new JsonResponse<>(response.code(), response.message(), response.headers(), body); } catch (IOException e) { // OkHttp throws this after clearing the interrupt status // TODO: remove after updating to Okio 1.15.0+ if ((e instanceof InterruptedIOException) && "thread interrupted".equals(e.getMessage())) { Thread.currentThread().interrupt(); } throw new UncheckedIOException(e); } }
static ResponseBody buffer(final ResponseBody body) throws IOException { Buffer buffer = new Buffer(); body.source().readAll(buffer); return ResponseBody.create(body.contentType(), body.contentLength(), buffer); }
/** * Peeks up to {@code byteCount} bytes from the response body and returns them as a new response * body. If fewer than {@code byteCount} bytes are in the response body, the full response body is * returned. If more than {@code byteCount} bytes are in the response body, the returned value * will be truncated to {@code byteCount} bytes. * * <p>It is an error to call this method after the body has been consumed. * * <p><strong>Warning:</strong> this method loads the requested bytes into memory. Most * applications should set a modest limit on {@code byteCount}, such as 1 MiB. */ public ResponseBody peekBody(long byteCount) throws IOException { BufferedSource peeked = body.source().peek(); Buffer buffer = new Buffer(); peeked.request(byteCount); buffer.write(peeked, Math.min(byteCount, peeked.getBuffer().size())); return ResponseBody.create(body.contentType(), buffer.size(), buffer); }
/** like {@link #get(Request)} except doesn't throw unsupported on not found */ Response call(Request request) throws IOException { try (Response response = client.newCall(request).execute()) { if (!HttpHeaders.hasBody(response)) return response; // buffer response so tests can read it. Otherwise the finally block will drop it ResponseBody toReturn; try (ResponseBody body = response.body()) { Buffer buffer = new Buffer(); body.source().readAll(buffer); toReturn = ResponseBody.create(body.contentType(), body.contentLength(), buffer); } return response.newBuilder().body(toReturn).build(); } }
public void processResponse(Response response) { try { if (!response.isSuccessful()) { listener.onFailure(this, null, response); return; } ResponseBody body = response.body(); //noinspection ConstantConditions main body is never null MediaType contentType = body.contentType(); if (!isEventStream(contentType)) { listener.onFailure(this, new IllegalStateException("Invalid content-type: " + contentType), response); return; } // Replace the body with an empty one so the callbacks can't see real data. response = response.newBuilder().body(Util.EMPTY_RESPONSE).build(); ServerSentEventReader reader = new ServerSentEventReader(body.source(), this); try { listener.onOpen(this, response); while (reader.processNextEvent()) { } } catch (Exception e) { listener.onFailure(this, e, response); return; } listener.onClosed(this); } finally { response.close(); } }
.body(new NoContentResponseBody(rawBody.contentType(), rawBody.contentLength())) .build();