List of usage examples for com.squareup.okhttp ResponseBody ResponseBody
ResponseBody
From source file:com.parse.ParseOkHttpClient.java
License:Open Source License
/** * For OKHttpClient, since it does not expose any interface for us to check the raw response * stream, we have to use OKHttp networkInterceptors. Instead of using our own interceptor list, * we use OKHttp inner interceptor list. * @param parseNetworkInterceptor//from w ww .ja va 2s . c om */ @Override /* package */ void addExternalInterceptor(final ParseNetworkInterceptor parseNetworkInterceptor) { okHttpClient.networkInterceptors().add(new Interceptor() { @Override public Response intercept(final Chain okHttpChain) throws IOException { Request okHttpRequest = okHttpChain.request(); // Transfer OkHttpRequest to ParseHttpRequest final ParseHttpRequest parseRequest = getParseHttpRequest(okHttpRequest); // Capture OkHttpResponse final Capture<Response> okHttpResponseCapture = new Capture<>(); final ParseHttpResponse parseResponse = parseNetworkInterceptor .intercept(new ParseNetworkInterceptor.Chain() { @Override public ParseHttpRequest getRequest() { return parseRequest; } @Override public ParseHttpResponse proceed(ParseHttpRequest parseRequest) throws IOException { // Use OKHttpClient to send request Request okHttpRequest = ParseOkHttpClient.this.getRequest(parseRequest); Response okHttpResponse = okHttpChain.proceed(okHttpRequest); okHttpResponseCapture.set(okHttpResponse); return getResponse(okHttpResponse); } }); final Response okHttpResponse = okHttpResponseCapture.get(); // Ideally we should build newOkHttpResponse only based on parseResponse, however // ParseHttpResponse does not have all the info we need to build the newOkHttpResponse, so // we rely on the okHttpResponse to generate the builder and change the necessary info // inside Response.Builder newOkHttpResponseBuilder = okHttpResponse.newBuilder(); // Set status newOkHttpResponseBuilder.code(parseResponse.getStatusCode()) .message(parseResponse.getReasonPhrase()); // Set headers if (parseResponse.getAllHeaders() != null) { for (Map.Entry<String, String> entry : parseResponse.getAllHeaders().entrySet()) { newOkHttpResponseBuilder.header(entry.getKey(), entry.getValue()); } } // Set body newOkHttpResponseBuilder.body(new ResponseBody() { @Override public MediaType contentType() { if (parseResponse.getContentType() == null) { return null; } return MediaType.parse(parseResponse.getContentType()); } @Override public long contentLength() throws IOException { return parseResponse.getTotalSize(); } @Override public BufferedSource source() throws IOException { // We need to use the proxy stream from interceptor to replace the origin network // stream, so when the stream is read by Parse, the network stream is proxyed in the // interceptor. if (parseResponse.getContent() == null) { return null; } return Okio.buffer(Okio.source(parseResponse.getContent())); } }); return newOkHttpResponseBuilder.build(); } }); }
From source file:com.parse.ParseOkHttpClientTest.java
License:Open Source License
@Test public void testGetParseResponse() throws IOException { int statusCode = 200; String reasonPhrase = "test reason"; final String content = "test"; final int contentLength = content.length(); final String contentType = "application/json"; String url = "http://www.parse.com/"; Request request = new Request.Builder().url(url).build(); Response okHttpResponse = new Response.Builder().request(request).protocol(Protocol.HTTP_1_1) .code(statusCode).message(reasonPhrase).body(new ResponseBody() { @Override/*from ww w .ja v a2s. c o m*/ public MediaType contentType() { return MediaType.parse(contentType); } @Override public long contentLength() throws IOException { return contentLength; } @Override public BufferedSource source() throws IOException { Buffer buffer = new Buffer(); buffer.write(content.getBytes()); return buffer; } }).build(); ParseOkHttpClient parseClient = new ParseOkHttpClient(10000, null); ParseHttpResponse parseResponse = parseClient.getResponse(okHttpResponse); // Verify status code assertEquals(statusCode, parseResponse.getStatusCode()); // Verify reason phrase assertEquals(reasonPhrase, parseResponse.getReasonPhrase()); // Verify content length assertEquals(contentLength, parseResponse.getTotalSize()); // Verify content assertArrayEquals(content.getBytes(), ParseIOUtils.toByteArray(parseResponse.getContent())); }
From source file:com.xing.api.CallSpecTest.java
License:Apache License
@Test public void exceptionCatchingBodyThrows() throws Exception { ResponseBody throwingBody = new ResponseBody() { @Override//from ww w . jav a2 s . c o m public MediaType contentType() { //noinspection ConstantConditions return MediaType.parse("application/h"); } @Override public long contentLength() throws IOException { throw new IOException("Broken body!"); } @Override public BufferedSource source() throws IOException { throw new IOException("Broken body!"); } }; // Test content length throws ExceptionCatchingRequestBody body = new ExceptionCatchingRequestBody(throwingBody); assertThat(body.contentType()).isEqualTo(throwingBody.contentType()); try { body.contentLength(); } catch (IOException ignored) { } try { body.throwIfCaught(); } catch (IOException e) { assertThat(e.getMessage()).isEqualTo("Broken body!"); } // Test source throws, here we need new object body = new ExceptionCatchingRequestBody(throwingBody); assertThat(body.contentType()).isEqualTo(throwingBody.contentType()); try { body.source(); } catch (IOException ignored) { } try { body.throwIfCaught(); } catch (IOException e) { assertThat(e.getMessage()).isEqualTo("Broken body!"); } }
From source file:qm.vp.kiev.qmhttplib.retrofit.Utils.java
License:Apache License
/** * Replace a {@link com.squareup.okhttp.Response} with an identical copy whose body is backed by a * {@link okio.Buffer} rather than a {@link okio.Source}. *///from w w w. j a va 2 s . c o m static Response readBodyToBytesIfNecessary(Response response) throws IOException { final ResponseBody body = response.body(); if (body == null) { return response; } BufferedSource source = body.source(); final Buffer buffer = new Buffer(); buffer.writeAll(source); source.close(); return response.newBuilder().body(new ResponseBody() { @Override public MediaType contentType() { return body.contentType(); } @Override public long contentLength() { return buffer.size(); } @Override public BufferedSource source() { return buffer.clone(); } }).build(); }