Java Code Examples for com.squareup.okhttp.ResponseBody#byteStream()

The following examples show how to use com.squareup.okhttp.ResponseBody#byteStream() . 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: HttpApiBase.java    From iview-android-tv with MIT License 5 votes vote down vote up
protected InputStream fetchStream(Uri url, int staleness) {
    ensureCache();
    ResponseBody body = fetchFromNetwork(url, staleness);
    if (body != null) {
        try {
            return body.byteStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}
 
Example 2
Source File: OkHttpImageDownloader.java    From Android-Universal-Image-Loader-Modify with Apache License 2.0 5 votes vote down vote up
@Override
protected InputStream getStreamFromNetwork(String imageUri, Object extra) throws IOException {
	Request request = new Request.Builder().url(imageUri).build();
	ResponseBody responseBody = client.newCall(request).execute().body();
	InputStream inputStream = responseBody.byteStream();
	int contentLength = (int) responseBody.contentLength();
	return new ContentLengthInputStream(inputStream, contentLength);
}
 
Example 3
Source File: OkHttpInterceptor.java    From weex with Apache License 2.0 4 votes vote down vote up
@Override
public Response intercept(Chain chain) throws IOException {
  String requestId = String.valueOf(mNextRequestId.getAndIncrement());

  Request request = chain.request();

  RequestBodyHelper requestBodyHelper = null;
  if (mEventReporter.isEnabled()) {
    requestBodyHelper = new RequestBodyHelper(mEventReporter, requestId);
    OkHttpInspectorRequest inspectorRequest =
        new OkHttpInspectorRequest(requestId, request, requestBodyHelper);
    mEventReporter.requestWillBeSent(inspectorRequest);
  }

  Response response;
  try {
    response = chain.proceed(request);
  } catch (IOException e) {
    if (mEventReporter.isEnabled()) {
      mEventReporter.httpExchangeFailed(requestId, e.toString());
    }
    throw e;
  }

  if (mEventReporter.isEnabled()) {
    if (requestBodyHelper != null && requestBodyHelper.hasBody()) {
      requestBodyHelper.reportDataSent();
    }

    Connection connection = chain.connection();
    mEventReporter.responseHeadersReceived(
        new OkHttpInspectorResponse(
            requestId,
            request,
            response,
            connection));

    ResponseBody body = response.body();
    MediaType contentType = null;
    InputStream responseStream = null;
    if (body != null) {
      contentType = body.contentType();
      responseStream = body.byteStream();
    }

    responseStream = mEventReporter.interpretResponseStream(
        requestId,
        contentType != null ? contentType.toString() : null,
        response.header("Content-Encoding"),
        responseStream,
        new DefaultResponseHandler(mEventReporter, requestId));
    if (responseStream != null) {
      response = response.newBuilder()
          .body(new ForwardingResponseBody(body, responseStream))
          .build();
    }
  }

  return response;
}
 
Example 4
Source File: StethoInterceptor.java    From stetho with MIT License 4 votes vote down vote up
@Override
public Response intercept(Chain chain) throws IOException {
  String requestId = mEventReporter.nextRequestId();

  Request request = chain.request();

  RequestBodyHelper requestBodyHelper = null;
  if (mEventReporter.isEnabled()) {
    requestBodyHelper = new RequestBodyHelper(mEventReporter, requestId);
    OkHttpInspectorRequest inspectorRequest =
        new OkHttpInspectorRequest(requestId, request, requestBodyHelper);
    mEventReporter.requestWillBeSent(inspectorRequest);
  }

  Response response;
  try {
    response = chain.proceed(request);
  } catch (IOException e) {
    if (mEventReporter.isEnabled()) {
      mEventReporter.httpExchangeFailed(requestId, e.toString());
    }
    throw e;
  }

  if (mEventReporter.isEnabled()) {
    if (requestBodyHelper != null && requestBodyHelper.hasBody()) {
      requestBodyHelper.reportDataSent();
    }

    Connection connection = chain.connection();
    if (connection == null) {
      throw new IllegalStateException(
          "No connection associated with this request; " +
              "did you use addInterceptor instead of addNetworkInterceptor?");
    }
    mEventReporter.responseHeadersReceived(
        new OkHttpInspectorResponse(
            requestId,
            request,
            response,
            connection));

    ResponseBody body = response.body();
    MediaType contentType = null;
    InputStream responseStream = null;
    if (body != null) {
      contentType = body.contentType();
      responseStream = body.byteStream();
    }

    responseStream = mEventReporter.interpretResponseStream(
        requestId,
        contentType != null ? contentType.toString() : null,
        response.header("Content-Encoding"),
        responseStream,
        new DefaultResponseHandler(mEventReporter, requestId));
    if (responseStream != null) {
      response = response.newBuilder()
          .body(new ForwardingResponseBody(body, responseStream))
          .build();
    }
  }

  return response;
}
 
Example 5
Source File: HttpClient.java    From zsync4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
static InputStream inputStream(Response response, ResourceTransferListener<Response> listener) throws IOException {
  final ResponseBody body = response.body();
  final InputStream in = body.byteStream();
  return new ObservableResourceInputStream<>(in, listener, response, response.body().contentLength());
}