Search in sources :

Example 6 with HttpException

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;
}
Also used : Response(retrofit2.Response) JsonSyntaxException(com.google.gson.JsonSyntaxException) HttpException(retrofit2.HttpException) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) Nullable(android.support.annotation.Nullable)

Example 7 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 8 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 9 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)

Example 10 with HttpException

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);
}
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) AuthView(com.khmelenko.lab.varis.view.AuthView) 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)

Aggregations

HttpException (retrofit2.HttpException)11 Response (retrofit2.Response)6 SocketTimeoutException (java.net.SocketTimeoutException)5 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)4 Disposable (io.reactivex.disposables.Disposable)4 UnknownHostException (java.net.UnknownHostException)4 JsonSyntaxException (com.google.gson.JsonSyntaxException)3 AccessTokenRequest (com.khmelenko.lab.varis.network.request.AccessTokenRequest)3 AuthorizationRequest (com.khmelenko.lab.varis.network.request.AuthorizationRequest)3 AccessToken (com.khmelenko.lab.varis.network.response.AccessToken)3 Authorization (com.khmelenko.lab.varis.network.response.Authorization)3 IOException (java.io.IOException)3 List (java.util.List)3 Test (org.junit.Test)3 HttpException (retrofit2.adapter.rxjava.HttpException)3 TextUtils (android.text.TextUtils)2 Gson (com.google.gson.Gson)2 JsonParseException (com.google.gson.JsonParseException)2 MvpPresenter (com.khmelenko.lab.varis.mvp.MvpPresenter)2 GitHubRestClient (com.khmelenko.lab.varis.network.retrofit.github.GitHubRestClient)2