- Add the Codota plugin to your IDE and get smart completions
private void myMethod () {}
private String convertStatusCode(HttpException httpException) { String msg; if (httpException.code() == 500) { msg = "服务器发生错误"; } else if (httpException.code() == 404) { msg = "请求地址不存在"; } else if (httpException.code() == 403) { msg = "请求被服务器拒绝"; } else if (httpException.code() == 401) { msg = "未授权"; } else if (httpException.code() == 307) { msg = "请求被重定向到其他页面"; } else { msg = httpException.message(); } return msg; } }
/** * Returns true if the Throwable is an instance of RetrofitError with an * http status code equals to the given one. */ public static boolean isHttpStatusCode(Throwable throwable, int statusCode) { return throwable instanceof HttpException && ((HttpException) throwable).code() == statusCode; }
private String convertStatusCode(HttpException httpException) { String msg; if (httpException.code() == 500) { msg = "服务器发生错误"; } else if (httpException.code() == 404) { msg = "请求地址不存在"; } else if (httpException.code() == 403) { msg = "请求被服务器拒绝"; } else if (httpException.code() == 307) { msg = "请求被重定向到其他页面"; } else { msg = httpException.message(); } return msg; } }
@Override public void accept(@NonNull Throwable throwable) throws Exception { if (throwable instanceof HttpException) { if (((HttpException) throwable).code() == HttpURLConnection.HTTP_NOT_FOUND) { if (getScene() != null) { getScene().showErrorDialog("Lost!"); } } else if (((HttpException) throwable).code() == HttpURLConnection.HTTP_UNAVAILABLE) { if (getScene() != null) { getScene().showErrorDialog("Fire on the Server"); } } else if (((HttpException) throwable).code() == HttpURLConnection.HTTP_UNAUTHORIZED) { if (getScene() != null) { getScene().showErrorDialog("You shall not pass!"); } } } else { if (getScene() != null) { getScene().showErrorDialog(throwable.getMessage()); } } throwable.printStackTrace(); } });
@Override public void onError(Throwable e) { if (e instanceof HttpException) { viewLayer.showError("StatusCode: " + ((HttpException) e).code()); } else if (e instanceof GeneralApiException) { viewLayer.showError(((GeneralApiException) e).message); } else { viewLayer.showError(e.getMessage()); } }
@Override public void onError(Throwable e) { if (e instanceof HttpException) { viewLayer.showError("StatusCode: " + ((HttpException) e).code()); } else if (e instanceof GeneralApiException) { viewLayer.showError(((GeneralApiException) e).message); } else { viewLayer.showError(e.getMessage()); } }
@Override public void onError(Throwable e) { String errorMessage; if (e instanceof HttpException) { errorMessage = SZApplication.getInstance().getResources().getString(R.string.network_error); String detail = getErrorDetail((HttpException) e); if (!TextUtils.isEmpty(detail)) { errorMessage = detail; } Log.e(TAG, "网络错误,code:" + ((HttpException) e).code(), e); } else if (e instanceof SocketTimeoutException) { errorMessage = SZApplication.getInstance().getResources().getString(R.string.time_out_error); Log.e(TAG, "连接超时", e); } else if (e instanceof ConnectException) { errorMessage = SZApplication.getInstance().getResources().getString(R.string.network_error); Log.e(TAG, "网络错误", e); } else if (e instanceof JsonParseException || e instanceof JSONException || e instanceof ParseException) { errorMessage = SZApplication.getInstance().getResources().getString(R.string.data_error); Log.e(TAG, "数据错误", e); } else { errorMessage = SZApplication.getInstance().getResources().getString(R.string.network_error); Log.e(TAG, "网络错误", e); } onErrorResponse(errorMessage); }
@Test public void testDemoResponseError401() { reset(mainSceneMock); MainPresenterImpl presenter = new MainPresenterImpl(schedulersProvider, pokeDataSource); when(httpException.code()).thenReturn(HttpURLConnection.HTTP_UNAUTHORIZED); when(pokeDataSource.getPokemonAbilityStringObservable(anyString())) .thenReturn(Observable.<String>error(httpException)); presenter.onSceneAdded(mainSceneMock, null); testScheduler.triggerActions(); verify(mainSceneMock, times(1)).showErrorDialog("You shall not pass!"); verify(mainSceneMock, times(0)).setApiText(anyString()); }
@Test public void testDemoResponseError404() { reset(mainSceneMock); MainPresenterImpl presenter = new MainPresenterImpl(schedulersProvider, pokeDataSource); when(httpException.code()).thenReturn(HttpURLConnection.HTTP_NOT_FOUND); when(pokeDataSource.getPokemonAbilityStringObservable(anyString())) .thenReturn(Observable.<String>error(httpException)); presenter.onSceneAdded(mainSceneMock, null); testScheduler.triggerActions(); verify(mainSceneMock, times(1)).showErrorDialog("Lost!"); verify(mainSceneMock, times(0)).setApiText(anyString()); }
@Test public void testDemoResponseError503() { reset(mainSceneMock); MainPresenterImpl presenter = new MainPresenterImpl(schedulersProvider, pokeDataSource); when(httpException.code()).thenReturn(HttpURLConnection.HTTP_UNAVAILABLE); when(pokeDataSource.getPokemonAbilityStringObservable(anyString())) .thenReturn(Observable.<String>error(httpException)); presenter.onSceneAdded(mainSceneMock, null); testScheduler.triggerActions(); verify(mainSceneMock, times(0)).setApiText(anyString()); verify(mainSceneMock, times(1)).showErrorDialog("Fire on the Server"); }
int code = ((HttpException) e).code(); if (code == 504) {
@SuppressWarnings({"ThrowableResultOfMethodCallIgnored", "ConstantConditions", "deprecation"}) private static boolean isHttpException(Throwable cause, int httpCode) { if (isException(cause, retrofit2.HttpException.class)) { retrofit2.HttpException httpException = (retrofit2.HttpException) getCause(cause, retrofit2.HttpException.class); return httpCode == httpException.code(); } if (isException(cause, retrofit2.adapter.rxjava2.HttpException.class)) { retrofit2.adapter.rxjava2.HttpException httpException = (retrofit2.adapter.rxjava2.HttpException) getCause(cause, retrofit2.adapter.rxjava2.HttpException.class); return httpCode == httpException.code(); } return false; }
@SuppressWarnings({"ThrowableResultOfMethodCallIgnored", "ConstantConditions", "deprecation"}) private static boolean isHttpException(Throwable cause, int httpCode) { if (isException(cause, retrofit2.HttpException.class)) { retrofit2.HttpException httpException = (retrofit2.HttpException) getCause(cause, retrofit2.HttpException.class); return httpCode == httpException.code(); } if (isException(cause, retrofit2.adapter.rxjava2.HttpException.class)) { retrofit2.adapter.rxjava2.HttpException httpException = (retrofit2.adapter.rxjava2.HttpException) getCause(cause, retrofit2.adapter.rxjava2.HttpException.class); return httpCode == httpException.code(); } return false; }
@Override public void onError(Throwable e) { MvpPresenter<? extends MvpView> presenter = mPresenterRef.get(); if ( (null == presenter ) || ! presenter.isViewAttached()) { return; } presenter.getMvpView().showLoading( false ); if (e instanceof HttpException) { ResponseBody responseBody = ((HttpException) e).response().errorBody(); presenter.getMvpView().onError( String.format( "HttpCode:%d\n\n%s", ((HttpException) e).code(), getErrorMessage(responseBody))); } else if (e instanceof SocketTimeoutException) { presenter.getMvpView().onError(R.string.timeout); } else if (e instanceof IOException) { presenter.getMvpView().onError(R.string.network_err); } else { e.printStackTrace(); presenter.getMvpView().onError(e.getMessage()); } }
protected void notifyErrorToMvpView( Throwable e){ getMvpView().showLoading(false); if (e instanceof HttpException) { ResponseBody responseBody = ((HttpException) e).response().errorBody(); getMvpView().onError( String.format( "HttpCode:%d\n\n%s", ((HttpException) e).code(), getErrorMessage(responseBody))); } else if (e instanceof SocketTimeoutException) { getMvpView().onError(R.string.timeout); } else if (e instanceof IOException) { getMvpView().onError(R.string.network_err); } else { getMvpView().onError(e.getMessage()); } }
ex = new ApiException(e, ApiCode.Request.HTTP_ERROR); switch (httpException.code()) { case ApiCode.Http.INTERNAL_SERVER_ERROR: ex.message = "服务器内部错误,错误码:" + ApiCode.Http.INTERNAL_SERVER_ERROR;
} else if (e instanceof HttpException) { HttpException httpException = (HttpException) e; switch (httpException.code()) { case HTTP_BAD_REQUEST: case HTTP_FORBIDDEN:
(retrofit2.HttpException) getCause(cause, retrofit2.HttpException.class); message = httpCode2MessageResource(httpException.code());
if (e instanceof HttpException) { HttpException httpException = (HttpException) e; ex = new ApiException(httpException, httpException.code()); ex.message = httpException.getMessage(); } else if (e instanceof SocketTimeoutException) {
if (e instanceof HttpException) { //HTTP错误 HttpException httpExc = (HttpException) e; ex = new ApiException(e, httpExc.code()); ex.setMsg("网络错误"); //均视为网络错误 return ex;