Search in sources :

Example 1 with HttpException

use of retrofit2.HttpException in project Varis-Android by dkhmelenko.

the class BuildsDetailsPresenter method startLoadingLog.

/**
     * Starts loading log file
     *
     * @param jobId Job ID
     */
public void startLoadingLog(long jobId) {
    mJobId = jobId;
    String accessToken = AppSettings.getAccessToken();
    Single<String> responseSingle;
    if (TextUtils.isEmpty(accessToken)) {
        responseSingle = mRawClient.getApiService().getLog(String.valueOf(mJobId));
    } else {
        String auth = String.format("token %1$s", AppSettings.getAccessToken());
        responseSingle = mRawClient.getApiService().getLog(auth, String.valueOf(mJobId));
    }
    Disposable subscription = responseSingle.subscribeOn(Schedulers.io()).map(s -> mRawClient.getLogUrl(mJobId)).onErrorResumeNext(new Function<Throwable, SingleSource<String>>() {

        @Override
        public SingleSource<String> apply(@NonNull Throwable throwable) throws Exception {
            String redirectUrl = "";
            HttpException httpException = (HttpException) throwable;
            Headers headers = httpException.response().headers();
            for (String header : headers.names()) {
                if (header.equals("Location")) {
                    redirectUrl = headers.get(header);
                    break;
                }
            }
            return Single.just(redirectUrl);
        }
    }).retry(LOAD_LOG_MAX_ATTEMPT).observeOn(AndroidSchedulers.mainThread()).subscribe((logUrl, throwable) -> {
        if (throwable == null) {
            getView().setLogUrl(logUrl);
        } else {
            getView().showLogError();
            getView().showLoadingError(throwable.getMessage());
        }
    });
    mSubscriptions.add(subscription);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Function(io.reactivex.functions.Function) Headers(okhttp3.Headers) NonNull(io.reactivex.annotations.NonNull) HttpException(retrofit2.HttpException)

Example 2 with HttpException

use of retrofit2.HttpException in project Mvp-Rxjava-Retrofit-dagger2 by pengMaster.

the class ErrorListenerImpl method handleResponseError.

@Override
public void handleResponseError(Context context, Throwable t) {
    Timber.tag("Catch-Error").w(t.getMessage());
    // 这里不光是只能打印错误,还可以根据不同的错误作出不同的逻辑处理
    String msg = "未知错误";
    if (t instanceof UnknownHostException) {
        msg = "网络不可用";
    } else if (t instanceof SocketTimeoutException) {
        msg = "请求网络超时";
    } else if (t instanceof HttpException) {
        HttpException httpException = (HttpException) t;
        msg = convertStatusCode(httpException);
    } else if (t instanceof JsonParseException || t instanceof ParseException || t instanceof JSONException) {
        msg = "数据解析错误";
    }
    // ArmsUtils.snackbarText(msg);
    ToastUtils.showShort(msg);
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) UnknownHostException(java.net.UnknownHostException) JSONException(org.json.JSONException) HttpException(retrofit2.HttpException) JsonParseException(com.google.gson.JsonParseException) ParseException(android.net.ParseException) JsonParseException(com.google.gson.JsonParseException)

Example 3 with HttpException

use of retrofit2.HttpException in project SmartCampus by Vegen.

the class HttpError method getErrorMessage.

public static String getErrorMessage(Throwable throwable) {
    LogUtils.e(throwable.getMessage());
    if (BuildConfig.DEBUG) {
        return throwable.getMessage();
    }
    if (throwable instanceof HttpException) {
        try {
            HttpException httpException = (HttpException) throwable;
            String errorMsg = httpException.response().errorBody().string();
            HashMap data = new Gson().fromJson(errorMsg, HashMap.class);
            if (data.containsKey("msg")) {
                return (String) data.get("msg");
            } else {
                return httpException.getMessage();
            }
        } catch (IOException e) {
            LogUtils.e(e.getMessage());
        } catch (JsonSyntaxException e) {
            LogUtils.e(e.getMessage());
            return throwable.getMessage();
        } catch (Exception e) {
            return "请求错误";
        }
    } else if (throwable instanceof SocketTimeoutException || throwable instanceof UnknownHostException) {
        return "请检查网络是否连通";
    } else {
        return "请求错误";
    }
    return "请求错误";
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) SocketTimeoutException(java.net.SocketTimeoutException) UnknownHostException(java.net.UnknownHostException) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) HttpException(retrofit2.HttpException) IOException(java.io.IOException) JsonSyntaxException(com.google.gson.JsonSyntaxException) SocketTimeoutException(java.net.SocketTimeoutException) HttpException(retrofit2.HttpException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 4 with HttpException

use of retrofit2.HttpException in project DevRing by LJYcoder.

the class ExceptionHandler method handleException.

public static ResponseThrowable handleException(Throwable e) {
    ResponseThrowable responseThrowable;
    // Log.i("tag", "e.toString = " + e.toString());
    if (e instanceof HttpException) {
        HttpException httpException = (HttpException) e;
        responseThrowable = new ResponseThrowable(e, ERROR.HTTP_ERROR);
        switch(httpException.code()) {
            case UNAUTHORIZED:
            case FORBIDDEN:
            case NOT_FOUND:
            case REQUEST_TIMEOUT:
            case GATEWAY_TIMEOUT:
            case INTERNAL_SERVER_ERROR:
            case BAD_GATEWAY:
            case SERVICE_UNAVAILABLE:
            default:
                responseThrowable.code = httpException.code();
                responseThrowable.message = "网络错误";
                break;
        }
        return responseThrowable;
    } else if (e instanceof ServerException) {
        ServerException resultException = (ServerException) e;
        responseThrowable = new ResponseThrowable(resultException, resultException.code);
        responseThrowable.message = resultException.message;
        return responseThrowable;
    } else if (e instanceof JsonParseException || e instanceof JSONException || e instanceof ParseException) {
        responseThrowable = new ResponseThrowable(e, ERROR.PARSE_ERROR);
        responseThrowable.message = "解析错误";
        return responseThrowable;
    } else if (e instanceof ConnectException) {
        responseThrowable = new ResponseThrowable(e, ERROR.CONNECT_ERROR);
        responseThrowable.message = "连接失败";
        return responseThrowable;
    } else if (e instanceof javax.net.ssl.SSLHandshakeException) {
        responseThrowable = new ResponseThrowable(e, ERROR.SSL_ERROR);
        responseThrowable.message = "证书验证失败";
        return responseThrowable;
    } else {
        responseThrowable = new ResponseThrowable(e, ERROR.UNKNOWN);
        responseThrowable.message = "未知错误";
        return responseThrowable;
    }
}
Also used : JSONException(org.json.JSONException) HttpException(retrofit2.HttpException) JsonParseException(com.google.gson.JsonParseException) ParseException(android.net.ParseException) JsonParseException(com.google.gson.JsonParseException) ConnectException(java.net.ConnectException)

Example 5 with HttpException

use of retrofit2.HttpException in project EnableHands by LeviWGG.

the class BaseExceptionEngine method handleException.

public static HttpResult handleException(Throwable throwable) {
    Log.d("exception", "Exception error : " + throwable.getClass());
    HttpResult httpResult = new HttpResult();
    if (throwable instanceof HttpException || throwable instanceof ConnectException || throwable instanceof SocketTimeoutException) {
        // 均视为网络错误
        httpResult.setStatus(HTTP_STATUS_NET_ERROR);
        httpResult.setMsg("网络连接失败,请稍后再试");
    } else if (throwable instanceof JsonParseException || throwable instanceof JSONException || throwable instanceof ParseException) {
        httpResult.setStatus(HTTP_STATUS_PARSE_FAILED);
        httpResult.setMsg("加载失败,请稍后再试");
    } else {
        httpResult.setStatus(HTTP_STATUS_DEFAULT_ERROR);
        httpResult.setMsg("系统繁忙,请稍后再试");
    }
    return httpResult;
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) JSONException(org.json.JSONException) HttpResult(app.main.wangliwei.enablehands.bean.HttpResult) HttpException(retrofit2.HttpException) JsonParseException(com.google.gson.JsonParseException) ParseException(java.text.ParseException) JsonParseException(com.google.gson.JsonParseException) ConnectException(java.net.ConnectException)

Aggregations

HttpException (retrofit2.HttpException)27 HttpException (retrofit2.adapter.rxjava.HttpException)18 Test (org.junit.Test)14 Intent (android.content.Intent)10 JSONException (org.json.JSONException)10 Bundle (android.os.Bundle)9 JsonParseException (com.google.gson.JsonParseException)9 ConnectException (java.net.ConnectException)8 ParseException (android.net.ParseException)7 SocketTimeoutException (java.net.SocketTimeoutException)7 UnknownHostException (java.net.UnknownHostException)7 Response (retrofit2.Response)7 View (android.view.View)6 Build (com.github.vase4kin.teamcityapp.buildlist.api.Build)6 Disposable (io.reactivex.disposables.Disposable)6 IOException (java.io.IOException)6 TextView (android.widget.TextView)5 List (java.util.List)5 TextUtils (android.text.TextUtils)4 BuildCancelRequest (com.github.vase4kin.teamcityapp.build_details.api.BuildCancelRequest)4