Search in sources :

Example 31 with HttpException

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);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) HttpURLConnection(java.net.HttpURLConnection) AccessToken(com.khmelenko.lab.varis.network.response.AccessToken) AppSettings(com.khmelenko.lab.varis.storage.AppSettings) Arrays(java.util.Arrays) HttpException(retrofit2.HttpException) TextUtils(android.text.TextUtils) Response(retrofit2.Response) MvpPresenter(com.khmelenko.lab.varis.mvp.MvpPresenter) GithubApiService(com.khmelenko.lab.varis.network.retrofit.github.GithubApiService) Single(io.reactivex.Single) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) EncryptionUtils(com.khmelenko.lab.varis.util.EncryptionUtils) Inject(javax.inject.Inject) List(java.util.List) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) TravisRestClient(com.khmelenko.lab.varis.network.retrofit.travis.TravisRestClient) StringUtils(com.khmelenko.lab.varis.util.StringUtils) AccessTokenRequest(com.khmelenko.lab.varis.network.request.AccessTokenRequest) AuthorizationRequest(com.khmelenko.lab.varis.network.request.AuthorizationRequest) Schedulers(io.reactivex.schedulers.Schedulers) Authorization(com.khmelenko.lab.varis.network.response.Authorization) GitHubRestClient(com.khmelenko.lab.varis.network.retrofit.github.GitHubRestClient) HttpException(retrofit2.HttpException)

Example 32 with HttpException

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;
}
Also used : Response(retrofit2.Response)

Example 33 with HttpException

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);
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) UnknownHostException(java.net.UnknownHostException) JsonIOException(com.google.gson.JsonIOException) JSONException(org.json.JSONException) HttpException(retrofit2.HttpException) JsonParseException(com.google.gson.JsonParseException) ParseException(android.net.ParseException) JsonParseException(com.google.gson.JsonParseException)

Example 34 with HttpException

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

Example 35 with HttpException

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();
        }
    });
}
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)

Aggregations

HttpException (retrofit2.HttpException)32 HttpException (retrofit2.adapter.rxjava.HttpException)18 Test (org.junit.Test)14 JSONException (org.json.JSONException)12 Intent (android.content.Intent)11 JsonParseException (com.google.gson.JsonParseException)11 ConnectException (java.net.ConnectException)10 Bundle (android.os.Bundle)9 ParseException (android.net.ParseException)8 IOException (java.io.IOException)7 SocketTimeoutException (java.net.SocketTimeoutException)7 UnknownHostException (java.net.UnknownHostException)7 Response (retrofit2.Response)7 View (android.view.View)6 Build (com.github.vase4kin.teamcityapp.buildlist.api.Build)6 Disposable (io.reactivex.disposables.Disposable)6 List (java.util.List)6 TextView (android.widget.TextView)5 TextUtils (android.text.TextUtils)4 BuildCancelRequest (com.github.vase4kin.teamcityapp.build_details.api.BuildCancelRequest)4