Search in sources :

Example 6 with ResponseBody

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);
        }
    });
}
Also used : Response(com.squareup.okhttp.Response) Call(com.squareup.okhttp.Call) Request(com.squareup.okhttp.Request) IOException(java.io.IOException) IOException(java.io.IOException) BaseProducerContextCallbacks(com.facebook.imagepipeline.producers.BaseProducerContextCallbacks) ResponseBody(com.squareup.okhttp.ResponseBody)

Example 7 with ResponseBody

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);
}
Also used : ContentLengthInputStream(com.nostra13.universalimageloader.core.assist.ContentLengthInputStream) ContentLengthInputStream(com.nostra13.universalimageloader.core.assist.ContentLengthInputStream) InputStream(java.io.InputStream) Request(com.squareup.okhttp.Request) ResponseBody(com.squareup.okhttp.ResponseBody)

Example 8 with ResponseBody

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;
}
Also used : UnknownHostException(java.net.UnknownHostException) MediaType(com.squareup.okhttp.MediaType) Gson(com.google.gson.Gson) HttpException(retrofit.HttpException) UnknownHostException(java.net.UnknownHostException) HttpException(retrofit.HttpException) ResponseBody(com.squareup.okhttp.ResponseBody) ErrorResponse(com.boredream.bdcodehelper.entity.ErrorResponse)

Aggregations

ResponseBody (com.squareup.okhttp.ResponseBody)8 Request (com.squareup.okhttp.Request)4 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 MediaType (com.squareup.okhttp.MediaType)3 Response (com.squareup.okhttp.Response)3 ErrorResponse (com.boredream.bdcodehelper.entity.ErrorResponse)2 Gson (com.google.gson.Gson)2 File (java.io.File)2 UnknownHostException (java.net.UnknownHostException)2 HttpException (retrofit.HttpException)2 Intent (android.content.Intent)1 Uri (android.net.Uri)1 QAException (cn.jeesoft.qa.error.QAException)1 QAIOException (cn.jeesoft.qa.error.QAIOException)1 QANoFoundException (cn.jeesoft.qa.error.QANoFoundException)1 BaseProducerContextCallbacks (com.facebook.imagepipeline.producers.BaseProducerContextCallbacks)1 DefaultResponseHandler (com.facebook.stetho.inspector.network.DefaultResponseHandler)1 RequestBodyHelper (com.facebook.stetho.inspector.network.RequestBodyHelper)1 ContentLengthInputStream (com.nostra13.universalimageloader.core.assist.ContentLengthInputStream)1