use of retrofit2.adapter.rxjava.HttpException in project archi by ivacf.
the class MainViewModelTest method shouldSearchInvalidUsername.
@Test
public void shouldSearchInvalidUsername() {
String username = "invalidUsername";
TextView textView = new TextView(application);
textView.setText(username);
HttpException mockHttpException = new HttpException(Response.error(404, mock(ResponseBody.class)));
when(githubService.publicRepositories(username)).thenReturn(Observable.<List<Repository>>error(mockHttpException));
mainViewModel.onSearchAction(textView, EditorInfo.IME_ACTION_SEARCH, null);
verify(dataListener, never()).onRepositoriesChanged(anyListOf(Repository.class));
assertEquals(mainViewModel.infoMessage.get(), application.getString(R.string.error_username_not_found));
assertEquals(mainViewModel.infoMessageVisibility.get(), View.VISIBLE);
assertEquals(mainViewModel.progressVisibility.get(), View.INVISIBLE);
assertEquals(mainViewModel.recyclerViewVisibility.get(), View.INVISIBLE);
}
use of retrofit2.adapter.rxjava.HttpException in project Varis-Android by dkhmelenko.
the class BuildsDetailsPresenter method startLoadingLog.
/**
* Starts loading log file
*
* @param jobId Job ID
*/
public void startLoadingLog(long jobId) {
mJobId = jobId;
String accessToken = AppSettings.getAccessToken();
Single<String> responseSingle;
if (TextUtils.isEmpty(accessToken)) {
responseSingle = mRawClient.getApiService().getLog(String.valueOf(mJobId));
} else {
String auth = String.format("token %1$s", AppSettings.getAccessToken());
responseSingle = mRawClient.getApiService().getLog(auth, String.valueOf(mJobId));
}
Disposable subscription = responseSingle.subscribeOn(Schedulers.io()).map(s -> mRawClient.getLogUrl(mJobId)).onErrorResumeNext(new Function<Throwable, SingleSource<String>>() {
@Override
public SingleSource<String> apply(@NonNull Throwable throwable) throws Exception {
String redirectUrl = "";
HttpException httpException = (HttpException) throwable;
Headers headers = httpException.response().headers();
for (String header : headers.names()) {
if (header.equals("Location")) {
redirectUrl = headers.get(header);
break;
}
}
return Single.just(redirectUrl);
}
}).retry(LOAD_LOG_MAX_ATTEMPT).observeOn(AndroidSchedulers.mainThread()).subscribe((logUrl, throwable) -> {
if (throwable == null) {
getView().setLogUrl(logUrl);
} else {
getView().showLogError();
getView().showLoadingError(throwable.getMessage());
}
});
mSubscriptions.add(subscription);
}
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);
}
Aggregations