Search in sources :

Example 1 with HttpException

use of retrofit.HttpException in project bdcodehelper by boredream.

the class ErrorConstants 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)

Example 2 with HttpException

use of retrofit.HttpException 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

ErrorResponse (com.boredream.bdcodehelper.entity.ErrorResponse)2 Gson (com.google.gson.Gson)2 MediaType (com.squareup.okhttp.MediaType)2 ResponseBody (com.squareup.okhttp.ResponseBody)2 UnknownHostException (java.net.UnknownHostException)2 HttpException (retrofit.HttpException)2