use of retrofit2.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 {
if (throwable instanceof HttpException && isTwoFactorAuthRequired((HttpException) throwable)) {
mSecurityCodeInput = true;
getView().showTwoFactorAuth();
} else {
getView().showErrorMessage(throwable.getMessage());
}
}
});
mSubscriptions.add(subscription);
}
use of retrofit2.HttpException in project Varis-Android by dkhmelenko.
the class AuthPresenter method isTwoFactorAuthRequired.
private boolean isTwoFactorAuthRequired(HttpException exception) {
Response response = exception.response();
boolean twoFactorAuthRequired = false;
for (String header : response.headers().names()) {
if (GithubApiService.TWO_FACTOR_HEADER.equals(header)) {
twoFactorAuthRequired = true;
break;
}
}
return response.code() == HttpURLConnection.HTTP_UNAUTHORIZED && twoFactorAuthRequired;
}
use of retrofit2.HttpException in project MVPArms by JessYanCoding.
the class ResponseErrorListenerImpl method handleResponseError.
@Override
public void handleResponseError(Context context, Throwable t) {
Timber.tag("Catch-Error").w(t);
// 这里不光只能打印错误, 还可以根据不同的错误做出不同的逻辑处理
// 这里只是对几个常用错误进行简单的处理, 展示这个类的用法, 在实际开发中请您自行对更多错误进行更严谨的处理
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 || t instanceof JsonIOException) {
msg = "数据解析错误";
}
ArmsUtils.snackbarText(msg);
}
use of retrofit2.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.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();
}
});
}
Aggregations