use of com.jakewharton.retrofit2.adapter.rxjava2.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);
}
use of com.jakewharton.retrofit2.adapter.rxjava2.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);
}
use of com.jakewharton.retrofit2.adapter.rxjava2.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 "请求错误";
}
use of com.jakewharton.retrofit2.adapter.rxjava2.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;
}
}
use of com.jakewharton.retrofit2.adapter.rxjava2.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;
}
Aggregations