use of retrofit2.HttpException in project SmartCampus by Vegen.
the class HttpError method getErrorCode.
public static int getErrorCode(Throwable throwable) {
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 ((Double) data.get("code")).intValue();
} else {
return -1;
}
} catch (IOException e) {
LogUtils.e(e.getMessage());
} catch (JsonSyntaxException e) {
LogUtils.e(e.getMessage());
return -1;
} catch (Exception e) {
return -1;
}
}
return -1;
}
use of retrofit2.HttpException in project BaseProject by fly803.
the class ExceptionHandle method handleException.
public static ResponeThrowable handleException(Throwable e) {
ResponeThrowable ex;
if (e instanceof HttpException) {
HttpException httpException = (HttpException) e;
ex = new ResponeThrowable(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 ResponeThrowable(resultException, resultException.getCode());
ex.message = resultException.getMessage();
return ex;
} else if (e instanceof JsonParseException || e instanceof JSONException || e instanceof ParseException) {
ex = new ResponeThrowable(e, ERROR.PARSE_ERROR);
ex.message = "解析错误";
return ex;
} else if (e instanceof ConnectException) {
ex = new ResponeThrowable(e, ERROR.NETWORD_ERROR);
ex.message = "连接失败";
return ex;
} else if (e instanceof javax.net.ssl.SSLHandshakeException) {
ex = new ResponeThrowable(e, ERROR.SSL_ERROR);
ex.message = "证书验证失败";
return ex;
} else if (e instanceof ApiException) {
ex = new ResponeThrowable(e, ((ApiException) e).getCode());
ex.message = e.getMessage();
return ex;
} else {
ex = new ResponeThrowable(e, ERROR.UNKNOWN);
ex.message = "未知错误";
return ex;
}
}
use of retrofit2.HttpException in project AndroidComponent by funnyzhaov.
the class ExceptionHandle method handleException.
/**
* 异常的处理
*/
@NonNull
public static Throwable handleException(Throwable e) {
ResponseThrowable ex;
if (e instanceof HttpException) {
HttpException httpException = (HttpException) e;
ex = 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:
ex.message = "网络错误";
break;
}
return ex;
} else if (e instanceof ServerException) {
ServerException resultException = (ServerException) e;
ex = new ResponseThrowable(resultException, resultException.status);
ex.message = resultException.message;
return ex;
} else if (e instanceof JsonParseException || 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 javax.net.ssl.SSLHandshakeException) {
ex = new ResponseThrowable(e, ERROR.SSL_ERROR);
ex.message = "证书验证失败";
return ex;
} else if (e instanceof ConnectTimeoutException) {
ex = new ResponseThrowable(e, ERROR.TIMEOUT_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);
ex.message = "未知错误";
return ex;
}
}
use of retrofit2.HttpException in project talk-android by nextcloud.
the class CallsListController method fetchData.
private void fetchData(boolean fromBottomSheet) {
dispose(null);
callItems = new ArrayList<>();
roomsQueryDisposable = ncApi.getRooms(ApiUtils.getCredentials(userEntity.getUsername(), userEntity.getToken()), ApiUtils.getUrlForGetRooms(userEntity.getBaseUrl())).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(roomsOverall -> {
if (roomsOverall != null) {
for (int i = 0; i < roomsOverall.getOcs().getData().size(); i++) {
callItems.add(new CallItem(roomsOverall.getOcs().getData().get(i), userEntity));
}
adapter.updateDataSet(callItems, true);
Collections.sort(callItems, (callItem, t1) -> Long.compare(t1.getModel().getLastPing(), callItem.getModel().getLastPing()));
if (searchItem != null) {
searchItem.setVisible(callItems.size() > 0);
}
}
if (swipeRefreshLayout != null) {
swipeRefreshLayout.setRefreshing(false);
}
}, 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;
}
}
if (swipeRefreshLayout != null) {
swipeRefreshLayout.setRefreshing(false);
}
dispose(roomsQueryDisposable);
}, () -> {
dispose(roomsQueryDisposable);
if (swipeRefreshLayout != null) {
swipeRefreshLayout.setRefreshing(false);
}
if (fromBottomSheet) {
new Handler().postDelayed(() -> {
bottomSheet.setCancelable(true);
if (bottomSheet.isShowing()) {
bottomSheet.cancel();
}
}, 2500);
}
});
}
use of retrofit2.HttpException in project xabber-android by redsolution.
the class RetrofitErrorConverter method throwableToHttpError.
@Nullable
public static String throwableToHttpError(Throwable throwable) {
String errorMessage = null;
APIError error = null;
if (throwable instanceof HttpException) {
HttpException exception = (HttpException) throwable;
Response response = exception.response();
ResponseBody responseBody = response.errorBody();
if (responseBody != null) {
Converter<ResponseBody, APIError> converter = HttpApiManager.getRetrofit().responseBodyConverter(APIError.class, new Annotation[0]);
try {
error = converter.convert(responseBody);
} catch (IOException | JsonSyntaxException e) {
e.printStackTrace();
}
}
if (error != null) {
if (error.getDetail() != null)
errorMessage = error.getDetail();
else if (error.getDetails() != null)
errorMessage = error.getDetails();
else if (error.getEmail() != null && error.getEmail().size() > 0)
errorMessage = error.getEmail().get(0);
else if (error.getCredentials() != null && error.getCredentials().size() > 0)
errorMessage = error.getCredentials().get(0);
else if (error.getCode() != null && error.getCode().size() > 0)
errorMessage = error.getCode().get(0);
else if (error.getUsername() != null && error.getUsername().size() > 0)
errorMessage = error.getUsername().get(0);
else if (error.getPhone() != null && error.getPhone().size() > 0)
errorMessage = error.getPhone().get(0);
}
}
return errorMessage;
}
Aggregations