use of com.jakewharton.retrofit2.adapter.rxjava2.HttpException in project AndroidUtilLib by SiberiaDante.
the class NetException method throwable.
public static ResponseThrowable throwable(Throwable e) {
ResponseThrowable ex;
// }
if (e instanceof HttpException) {
HttpException httpException = (HttpException) e;
ex = new ResponseThrowable(e, ERROR.HTTP_ERROR);
switch(httpException.code()) {
case REQUEST_TIMEOUT:
case GATEWAY_TIMEOUT:
ex.message = "请求超时,请检查网络";
break;
case UNAUTHORIZED:
case FORBIDDEN:
case NOT_FOUND:
// 500
case INTERNAL_SERVER_ERROR:
case BAD_GATEWAY:
case SERVICE_UNAVAILABLE:
case ACCESS_DENIED:
ex.message = "网络异常";
break;
default:
ex.message = "网络错误";
break;
}
return ex;
} else if (e instanceof RuntimeException) {
ex = new ResponseThrowable(e, ERROR.RUNTIME);
ex.message = "RuntimeException: " + e.getMessage();
return ex;
} else if (e instanceof JSONException || e instanceof ParseException) {
ex = new ResponseThrowable(e, ERROR.PARSE_ERROR);
ex.message = "解析错误";
return ex;
} else if (e instanceof ConnectException) {
ex = new ResponseThrowable(e, ERROR.NETWORD_ERROR);
ex.message = "网络连接失败";
return ex;
} else if (e instanceof java.security.cert.CertPathValidatorException) {
ex = new ResponseThrowable(e, ERROR.SSL_NOT_FOUND);
ex.message = "证书路径没找到";
return ex;
} else if (e instanceof javax.net.ssl.SSLHandshakeException) {
ex = new ResponseThrowable(e, ERROR.SSL_ERROR);
ex.message = "证书验证失败";
return ex;
} else if (e instanceof java.net.SocketTimeoutException) {
ex = new ResponseThrowable(e, ERROR.TIMEOUT_ERROR);
ex.message = "连接超时,请稍后重试";
return ex;
} else {
ex = new ResponseThrowable(e, ERROR.UNKNOWN);
if (!SDNetWorkUtil.isNetWorkConnected()) {
ex.message = "网络未连接";
} else if (!SDNetWorkUtil.isAvailableByPing()) {
ex.message = "网络不可用";
} else {
ex.message = "未知错误";
}
return ex;
}
}
use of com.jakewharton.retrofit2.adapter.rxjava2.HttpException in project talk-android by nextcloud.
the class ContactsController method fetchData.
private void fetchData() {
dispose(null);
Set<Sharee> shareeHashSet = new HashSet<>();
contactItems = new ArrayList<>();
userHeaderItems = new HashMap<>();
RetrofitBucket retrofitBucket = ApiUtils.getRetrofitBucketForContactsSearch(userEntity.getBaseUrl(), "");
contactsQueryDisposable = ncApi.getContactsWithSearchParam(ApiUtils.getCredentials(userEntity.getUsername(), userEntity.getToken()), retrofitBucket.getUrl(), retrofitBucket.getQueryMap()).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe((ShareesOverall shareesOverall) -> {
if (shareesOverall != null) {
if (shareesOverall.getOcs().getData().getUsers() != null) {
shareeHashSet.addAll(shareesOverall.getOcs().getData().getUsers());
}
if (shareesOverall.getOcs().getData().getExactUsers() != null && shareesOverall.getOcs().getData().getExactUsers().getExactSharees() != null) {
shareeHashSet.addAll(shareesOverall.getOcs().getData().getExactUsers().getExactSharees());
}
Participant participant;
for (Sharee sharee : shareeHashSet) {
if (!sharee.getValue().getShareWith().equals(userEntity.getUsername())) {
participant = new Participant();
participant.setName(sharee.getLabel());
String headerTitle;
headerTitle = sharee.getLabel().substring(0, 1).toUpperCase();
UserHeaderItem userHeaderItem;
if (!userHeaderItems.containsKey(headerTitle)) {
userHeaderItem = new UserHeaderItem(headerTitle);
userHeaderItems.put(headerTitle, userHeaderItem);
}
participant.setUserId(sharee.getValue().getShareWith());
contactItems.add(new UserItem(participant, userEntity, userHeaderItems.get(headerTitle)));
}
}
userHeaderItems = new HashMap<>();
Collections.sort(contactItems, (o1, o2) -> {
String firstName;
String secondName;
if (o1 instanceof UserItem) {
firstName = ((UserItem) o1).getModel().getName();
} else {
firstName = ((UserHeaderItem) o1).getModel();
}
if (o2 instanceof UserItem) {
secondName = ((UserItem) o2).getModel().getName();
} else {
secondName = ((UserHeaderItem) o2).getModel();
}
return firstName.compareToIgnoreCase(secondName);
});
if (isNewConversationView) {
contactItems.add(0, new NewCallHeaderItem());
}
adapter.updateDataSet(contactItems, true);
searchItem.setVisible(contactItems.size() > 0);
swipeRefreshLayout.setRefreshing(false);
if (isNewConversationView) {
checkAndHandleBottomButtons();
}
}
}, throwable -> {
if (searchItem != null) {
searchItem.setVisible(false);
}
if (throwable instanceof HttpException) {
HttpException exception = (HttpException) throwable;
switch(exception.code()) {
case 401:
if (getParentController() != null && getParentController().getRouter() != null) {
getParentController().getRouter().pushController((RouterTransaction.with(new WebViewLoginController(userEntity.getBaseUrl(), true)).pushChangeHandler(new VerticalChangeHandler()).popChangeHandler(new VerticalChangeHandler())));
}
break;
default:
break;
}
}
swipeRefreshLayout.setRefreshing(false);
dispose(contactsQueryDisposable);
}, () -> {
swipeRefreshLayout.setRefreshing(false);
dispose(contactsQueryDisposable);
});
}
use of com.jakewharton.retrofit2.adapter.rxjava2.HttpException in project BaseProject by wareine.
the class ExceptionHandle method handleException.
public static ResponseException handleException(Throwable e) {
ResponseException ex;
if (e instanceof HttpException) {
HttpException httpException = (HttpException) e;
ex = new ResponseException(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:
ex.message = "网络错误";
break;
}
return ex;
} else if (e instanceof ServerException) {
ServerException resultException = (ServerException) e;
ex = new ResponseException(resultException, resultException.code);
ex.message = resultException.message;
return ex;
} else if (e instanceof JsonParseException || e instanceof JSONException || e instanceof ParseException) {
ex = new ResponseException(e, ERROR.PARSE_ERROR);
ex.message = "解析错误";
return ex;
} else if (e instanceof ConnectException) {
ex = new ResponseException(e, ERROR.NETWORD_ERROR);
ex.message = "连接失败";
return ex;
} else if (e instanceof javax.net.ssl.SSLHandshakeException) {
ex = new ResponseException(e, ERROR.SSL_ERROR);
ex.message = "证书验证失败";
return ex;
} else if (e instanceof ConnectTimeoutException) {
ex = new ResponseException(e, ERROR.TIMEOUT_ERROR);
ex.message = "连接超时";
return ex;
} else if (e instanceof java.net.SocketTimeoutException) {
ex = new ResponseException(e, ERROR.TIMEOUT_ERROR);
ex.message = "连接超时";
return ex;
} else {
ex = new ResponseException(e, ERROR.UNKNOWN);
ex.message = "未知错误";
return ex;
}
}
use of com.jakewharton.retrofit2.adapter.rxjava2.HttpException in project RxReddit by damien5314.
the class RedditAuthService method refreshUserAccessToken.
@Override
public Observable<UserAccessToken> refreshUserAccessToken() {
return Observable.defer(() -> {
UserAccessToken token = getUserAccessToken();
if (token == null) {
return Observable.error(new IllegalStateException("No user access token available"));
}
if (token.secondsUntilExpiration() > EXPIRATION_THRESHOLD) {
return Observable.just(token);
}
String refreshToken = token.getRefreshToken();
if (refreshToken == null) {
clearUserAccessToken();
return Observable.error(new IllegalStateException("No refresh token available"));
}
String grantType = "refresh_token";
return authService.refreshUserAuthToken(grantType, refreshToken).flatMap(RxRedditUtil::responseToBody).doOnNext(this::saveUserAccessToken).doOnError(error -> {
if (error instanceof HttpException && ((HttpException) error).code() == 403) {
// 403 means our refresh token is no longer good, just discard it
clearUserAccessToken();
}
});
});
}
use of com.jakewharton.retrofit2.adapter.rxjava2.HttpException in project bdcodehelper by boredream.
the class LcErrorConstants 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 != null && type.type().equals("application") && type.subtype().equals("json")) {
try {
// 这里的返回内容是Bmob/AVOS/Parse等RestFul API文档中的错误代码和错误信息对象
BaseResponse errorResponse = new Gson().fromJson(responseBody.string(), BaseResponse.class);
errorInfo = getLocalErrorInfo(errorResponse);
} catch (Exception e) {
e.printStackTrace();
}
}
} else if (throwable instanceof LcErrorResponse) {
LcErrorResponse lce = (LcErrorResponse) throwable;
errorInfo = getLocalErrorInfo(lce.getError());
} else {
if (throwable instanceof UnknownHostException) {
errorInfo = "无法连接到服务器";
}
}
return errorInfo;
}
Aggregations