本文整理汇总了Java中com.squareup.okhttp.ResponseBody.byteStream方法的典型用法代码示例。如果您正苦于以下问题:Java ResponseBody.byteStream方法的具体用法?Java ResponseBody.byteStream怎么用?Java ResponseBody.byteStream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.squareup.okhttp.ResponseBody
的用法示例。
在下文中一共展示了ResponseBody.byteStream方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getStreamFromNetwork
import com.squareup.okhttp.ResponseBody; //导入方法依赖的package包/类
@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);
}
示例2: downloadFile
import com.squareup.okhttp.ResponseBody; //导入方法依赖的package包/类
private boolean downloadFile(@NonNull String url, @NonNull File targetFile) {
LOGD(TAG, "Downloading \"" + url + "\" into \"" + targetFile + "\"...");
Request request = new Request.Builder().url(url).tag(OKHTTP_TAG).build();
Response response;
try {
response = mOkHttpClient.newCall(request).execute();
if (!response.isSuccessful()) {
return false;
}
final ResponseBody body = response.body();
final InputStream inputStream = body.byteStream();
final OutputStream outputStream = new FileOutputStream(targetFile);
byte[] buffer = new byte[BUFFER_SIZE];
int len;
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
mDownloaded += len;
publishProgress((int) (mDownloaded * 100 / mSelectedMap.getMapSize()));
}
inputStream.close();
outputStream.close();
} catch (IOException e) {
LOGE(TAG, "Cannot download map.", e);
return false;
}
LOGD(TAG, "Downloaded.");
return true;
}
示例3: fetchStream
import com.squareup.okhttp.ResponseBody; //导入方法依赖的package包/类
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;
}
示例4: intercept
import com.squareup.okhttp.ResponseBody; //导入方法依赖的package包/类
@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;
}