List of usage examples for com.squareup.okhttp Response.Builder body
ResponseBody body
To view the source code for com.squareup.okhttp Response.Builder body.
Click Source Link
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 ww w .j av a 2s.co m */ @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(); } }); }