use of retrofit2.adapter.rxjava.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.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;
}
use of retrofit2.adapter.rxjava.HttpException in project archi by ivacf.
the class MainPresenterTest method loadRepositoriesCallsShowMessage_withUsernameNotFoundString.
@Test
public void loadRepositoriesCallsShowMessage_withUsernameNotFoundString() {
String username = "ivacf";
HttpException mockHttpException = new HttpException(Response.error(404, mock(ResponseBody.class)));
when(githubService.publicRepositories(username)).thenReturn(Observable.<List<Repository>>error(mockHttpException));
mainPresenter.loadRepositories(username);
verify(mainMvpView).showProgressIndicator();
verify(mainMvpView).showMessage(R.string.error_username_not_found);
}
use of retrofit2.adapter.rxjava.HttpException in project archi by ivacf.
the class MainActivity method loadGithubRepos.
public void loadGithubRepos(String username) {
progressBar.setVisibility(View.VISIBLE);
reposRecycleView.setVisibility(View.GONE);
infoTextView.setVisibility(View.GONE);
ArchiApplication application = ArchiApplication.get(this);
GithubService githubService = application.getGithubService();
subscription = githubService.publicRepositories(username).observeOn(AndroidSchedulers.mainThread()).subscribeOn(application.defaultSubscribeScheduler()).subscribe(new Subscriber<List<Repository>>() {
@Override
public void onCompleted() {
progressBar.setVisibility(View.GONE);
if (reposRecycleView.getAdapter().getItemCount() > 0) {
reposRecycleView.requestFocus();
hideSoftKeyboard();
reposRecycleView.setVisibility(View.VISIBLE);
} else {
infoTextView.setText(R.string.text_empty_repos);
infoTextView.setVisibility(View.VISIBLE);
}
}
@Override
public void onError(Throwable error) {
Log.e(TAG, "Error loading GitHub repos ", error);
progressBar.setVisibility(View.GONE);
if (error instanceof HttpException && ((HttpException) error).code() == 404) {
infoTextView.setText(R.string.error_username_not_found);
} else {
infoTextView.setText(R.string.error_loading_repos);
}
infoTextView.setVisibility(View.VISIBLE);
}
@Override
public void onNext(List<Repository> repositories) {
Log.i(TAG, "Repos loaded " + repositories);
RepositoryAdapter adapter = (RepositoryAdapter) reposRecycleView.getAdapter();
adapter.setRepositories(repositories);
adapter.notifyDataSetChanged();
}
});
}
use of retrofit2.adapter.rxjava.HttpException in project BBS-Android by bdpqchen.
the class SimpleObserver method onError.
@Override
public void onError(Throwable throwable) {
LogUtil.dd("onError()");
// TODO: 17-4-27 无网络请求监听,扼杀在请求阶段
String msg = throwable.getMessage();
if (msg != null && msg.length() == 0) {
msg = "网络错误";
}
if (throwable instanceof SocketTimeoutException) {
msg = "网络请求超时...请重试";
} else if (throwable instanceof UnknownHostException) {
msg = "找不到服务器了..";
} else if (throwable instanceof ResponseException) {
msg = throwable.getMessage();
} else if (throwable instanceof HttpException) {
HttpException exception = (HttpException) throwable;
try {
String errorBody = exception.response().errorBody().string();
JSONObject errorJsonObject = new JSONObject(errorBody);
// int errCode = errorJsonObject.getInt("err");
msg = errorJsonObject.getString("data");
} catch (IOException | JSONException e) {
e.printStackTrace();
}
}
// LogUtil.dd("error type", String.valueOf(throwable.getCause()));
// LogUtil.dd("error message", String.valueOf(throwable.getMessage()));
// LogUtil.dd("error strackT", String.valueOf(throwable.getStackTrace()));
// LogUtil.dd("error strackT", String.valueOf(throwable.getLocalizedMessage()));
_onError(msg);
}
use of retrofit2.adapter.rxjava.HttpException in project Varis-Android by dkhmelenko.
the class AuthPresenter method doLogin.
private void doLogin(Single<Authorization> authorizationJob) {
Disposable subscription = authorizationJob.flatMap(this::doAuthorization).doOnSuccess(this::saveAccessToken).doAfterSuccess(accessToken -> cleanUpAfterAuthorization()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe((authorization, throwable) -> {
getView().hideProgress();
if (throwable == null) {
getView().finishView();
} else {
HttpException httpException = (HttpException) throwable;
if (isTwoFactorAuthRequired(httpException)) {
mSecurityCodeInput = true;
getView().showTwoFactorAuth();
} else {
getView().showErrorMessage(throwable.getMessage());
}
}
});
mSubscriptions.add(subscription);
}
Aggregations