use of com.squareup.okhttp.ResponseBody in project fresco by facebook.
the class OkHttpNetworkFetcher method fetchWithRequest.
protected void fetchWithRequest(final OkHttpNetworkFetchState fetchState, final Callback callback, final Request request) {
final Call call = mOkHttpClient.newCall(request);
fetchState.getContext().addCallbacks(new BaseProducerContextCallbacks() {
@Override
public void onCancellationRequested() {
if (Looper.myLooper() != Looper.getMainLooper()) {
call.cancel();
} else {
mCancellationExecutor.execute(new Runnable() {
@Override
public void run() {
call.cancel();
}
});
}
}
});
call.enqueue(new com.squareup.okhttp.Callback() {
@Override
public void onResponse(Response response) {
fetchState.responseTime = SystemClock.uptimeMillis();
final ResponseBody body = response.body();
try {
if (!response.isSuccessful()) {
handleException(call, new IOException("Unexpected HTTP code " + response), callback);
return;
}
long contentLength = body.contentLength();
if (contentLength < 0) {
contentLength = 0;
}
callback.onResponse(body.byteStream(), (int) contentLength);
} catch (Exception e) {
handleException(call, e, callback);
} finally {
try {
body.close();
} catch (Exception e) {
FLog.w(TAG, "Exception when closing response body", e);
}
}
}
@Override
public void onFailure(final Request request, final IOException e) {
handleException(call, e, callback);
}
});
}
use of com.squareup.okhttp.ResponseBody in project Android-Universal-Image-Loader by nostra13.
the class OkHttpImageDownloader method getStreamFromNetwork.
@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);
}
use of com.squareup.okhttp.ResponseBody in project bdcodehelper by boredream.
the class ErrorInfoUtils method parseHttpErrorInfo.
/**
* 解析服务器错误信息
*/
public static String parseHttpErrorInfo(Throwable throwable) {
String errorInfo = throwable.getMessage();
if (throwable instanceof HttpException) {
// 如果是Retrofit的Http错误,则转换类型,获取信息
HttpException exception = (HttpException) throwable;
ResponseBody responseBody = exception.response().errorBody();
MediaType type = responseBody.contentType();
// 如果是application/json类型数据,则解析返回内容
if (type.type().equals("application") && type.subtype().equals("json")) {
try {
// 这里的返回内容是Bmob/AVOS/Parse等RestFul API文档中的错误代码和错误信息对象
ErrorResponse errorResponse = new Gson().fromJson(responseBody.string(), ErrorResponse.class);
errorInfo = getLocalErrorInfo(errorResponse);
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
if (throwable instanceof UnknownHostException) {
errorInfo = "无法连接到服务器";
}
}
return errorInfo;
}
Aggregations