Search in sources :

Example 1 with HttpException

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);
}
Also used : Repository(uk.ivanc.archimvvm.model.Repository) TextView(android.widget.TextView) HttpException(retrofit2.adapter.rxjava.HttpException) Test(org.junit.Test)

Example 2 with HttpException

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);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Function(io.reactivex.functions.Function) Headers(okhttp3.Headers) NonNull(io.reactivex.annotations.NonNull) HttpException(retrofit2.HttpException)

Example 3 with HttpException

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);
}
Also used : Repository(uk.ivanc.archimvp.model.Repository) HttpException(retrofit2.adapter.rxjava.HttpException) Test(org.junit.Test)

Example 4 with HttpException

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();
        }
    });
}
Also used : GithubService(uk.ivanc.archi.model.GithubService) Repository(uk.ivanc.archi.model.Repository) Subscriber(rx.Subscriber) HttpException(retrofit2.adapter.rxjava.HttpException) List(java.util.List)

Example 5 with HttpException

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);
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) UnknownHostException(java.net.UnknownHostException) JSONObject(org.json.JSONObject) HttpException(retrofit2.HttpException)

Aggregations

HttpException (retrofit2.HttpException)3 HttpException (retrofit2.adapter.rxjava.HttpException)3 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)2 Disposable (io.reactivex.disposables.Disposable)2 List (java.util.List)2 Test (org.junit.Test)2 Response (retrofit2.Response)2 TextUtils (android.text.TextUtils)1 TextView (android.widget.TextView)1 MvpPresenter (com.khmelenko.lab.varis.mvp.MvpPresenter)1 AccessTokenRequest (com.khmelenko.lab.varis.network.request.AccessTokenRequest)1 AuthorizationRequest (com.khmelenko.lab.varis.network.request.AuthorizationRequest)1 AccessToken (com.khmelenko.lab.varis.network.response.AccessToken)1 Authorization (com.khmelenko.lab.varis.network.response.Authorization)1 GitHubRestClient (com.khmelenko.lab.varis.network.retrofit.github.GitHubRestClient)1 GithubApiService (com.khmelenko.lab.varis.network.retrofit.github.GithubApiService)1 TravisRestClient (com.khmelenko.lab.varis.network.retrofit.travis.TravisRestClient)1 AppSettings (com.khmelenko.lab.varis.storage.AppSettings)1 EncryptionUtils (com.khmelenko.lab.varis.util.EncryptionUtils)1 StringUtils (com.khmelenko.lab.varis.util.StringUtils)1