Search in sources :

Example 21 with HttpException

use of com.jakewharton.retrofit2.adapter.rxjava2.HttpException in project talk-android by nextcloud.

the class CallsListController method fetchData.

private void fetchData(boolean fromBottomSheet) {
    dispose(null);
    callItems = new ArrayList<>();
    roomsQueryDisposable = ncApi.getRooms(ApiUtils.getCredentials(userEntity.getUsername(), userEntity.getToken()), ApiUtils.getUrlForGetRooms(userEntity.getBaseUrl())).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(roomsOverall -> {
        if (roomsOverall != null) {
            for (int i = 0; i < roomsOverall.getOcs().getData().size(); i++) {
                callItems.add(new CallItem(roomsOverall.getOcs().getData().get(i), userEntity));
            }
            adapter.updateDataSet(callItems, true);
            Collections.sort(callItems, (callItem, t1) -> Long.compare(t1.getModel().getLastPing(), callItem.getModel().getLastPing()));
            if (searchItem != null) {
                searchItem.setVisible(callItems.size() > 0);
            }
        }
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setRefreshing(false);
        }
    }, throwable -> {
        if (searchItem != null) {
            searchItem.setVisible(false);
        }
        if (throwable instanceof HttpException) {
            HttpException exception = (HttpException) throwable;
            switch(exception.code()) {
                case 401:
                    if (getParentController() != null && getParentController().getRouter() != null) {
                        getParentController().getRouter().pushController((RouterTransaction.with(new WebViewLoginController(userEntity.getBaseUrl(), true)).pushChangeHandler(new VerticalChangeHandler()).popChangeHandler(new VerticalChangeHandler())));
                    }
                    break;
                default:
                    break;
            }
        }
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setRefreshing(false);
        }
        dispose(roomsQueryDisposable);
    }, () -> {
        dispose(roomsQueryDisposable);
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setRefreshing(false);
        }
        if (fromBottomSheet) {
            new Handler().postDelayed(() -> {
                bottomSheet.setCancelable(true);
                if (bottomSheet.isShowing()) {
                    bottomSheet.cancel();
                }
            }, 2500);
        }
    });
}
Also used : AutoInjector(autodagger.AutoInjector) Bundle(android.os.Bundle) UserUtils(com.nextcloud.talk.utils.database.user.UserUtils) EntryMenuController(com.nextcloud.talk.controllers.bottomsheet.EntryMenuController) SearchView(android.support.v7.widget.SearchView) CallMenuController(com.nextcloud.talk.controllers.bottomsheet.CallMenuController) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) DividerItemDecoration(android.support.v7.widget.DividerItemDecoration) BindView(butterknife.BindView) ApiUtils(com.nextcloud.talk.utils.ApiUtils) BottomSheet(com.kennyc.bottomsheet.BottomSheet) Handler(android.os.Handler) View(android.view.View) Schedulers(io.reactivex.schedulers.Schedulers) VerticalChangeHandler(com.bluelinelabs.conductor.changehandler.VerticalChangeHandler) Participant(com.nextcloud.talk.models.json.participants.Participant) MenuItemCompat(android.support.v4.view.MenuItemCompat) HttpException(retrofit2.HttpException) InputType(android.text.InputType) FlexibleAdapter(eu.davidea.flexibleadapter.FlexibleAdapter) ThreadMode(org.greenrobot.eventbus.ThreadMode) ViewGroup(android.view.ViewGroup) NoOpControllerChangeHandler(com.bluelinelabs.conductor.internal.NoOpControllerChangeHandler) BundleKeys(com.nextcloud.talk.utils.bundle.BundleKeys) List(java.util.List) Disposable(io.reactivex.disposables.Disposable) Parcels(org.parceler.Parcels) FastScroller(eu.davidea.fastscroller.FastScroller) CallItem(com.nextcloud.talk.adapters.items.CallItem) Nullable(android.support.annotation.Nullable) SearchManager(android.app.SearchManager) EditorInfo(android.view.inputmethod.EditorInfo) Context(android.content.Context) MoreMenuClickEvent(com.nextcloud.talk.events.MoreMenuClickEvent) UserEntity(com.nextcloud.talk.models.database.UserEntity) SmoothScrollLinearLayoutManager(eu.davidea.flexibleadapter.common.SmoothScrollLinearLayoutManager) Intent(android.content.Intent) NonNull(android.support.annotation.NonNull) MenuItem(android.view.MenuItem) ArrayList(java.util.ArrayList) BottomSheetLockEvent(com.nextcloud.talk.events.BottomSheetLockEvent) Inject(javax.inject.Inject) BaseController(com.nextcloud.talk.controllers.base.BaseController) MenuInflater(android.view.MenuInflater) EventBus(org.greenrobot.eventbus.EventBus) Menu(android.view.Menu) SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout) NcApi(com.nextcloud.talk.api.NcApi) BottomNavigationView(android.support.design.widget.BottomNavigationView) R(com.nextcloud.talk.R) Room(com.nextcloud.talk.models.json.rooms.Room) LayoutInflater(android.view.LayoutInflater) TextUtils(android.text.TextUtils) HorizontalChangeHandler(com.bluelinelabs.conductor.changehandler.HorizontalChangeHandler) RecyclerView(android.support.v7.widget.RecyclerView) Subscribe(org.greenrobot.eventbus.Subscribe) KeyboardUtils(com.nextcloud.talk.utils.KeyboardUtils) CallActivity(com.nextcloud.talk.activities.CallActivity) ViewTreeObserver(android.view.ViewTreeObserver) Collections(java.util.Collections) NextcloudTalkApplication(com.nextcloud.talk.application.NextcloudTalkApplication) RouterTransaction(com.bluelinelabs.conductor.RouterTransaction) Handler(android.os.Handler) VerticalChangeHandler(com.bluelinelabs.conductor.changehandler.VerticalChangeHandler) NoOpControllerChangeHandler(com.bluelinelabs.conductor.internal.NoOpControllerChangeHandler) HorizontalChangeHandler(com.bluelinelabs.conductor.changehandler.HorizontalChangeHandler) CallItem(com.nextcloud.talk.adapters.items.CallItem) HttpException(retrofit2.HttpException) VerticalChangeHandler(com.bluelinelabs.conductor.changehandler.VerticalChangeHandler)

Example 22 with HttpException

use of com.jakewharton.retrofit2.adapter.rxjava2.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.getDetails() != null)
                errorMessage = error.getDetails();
            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(androidx.annotation.Nullable)

Example 23 with HttpException

use of com.jakewharton.retrofit2.adapter.rxjava2.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 24 with HttpException

use of com.jakewharton.retrofit2.adapter.rxjava2.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 25 with HttpException

use of com.jakewharton.retrofit2.adapter.rxjava2.HttpException in project Auto.js by hyb1996.

the class NodeBB method getErrorMessage.

public static String getErrorMessage(Throwable e, Context context, String defaultMsg) {
    if (!(e instanceof HttpException)) {
        return defaultMsg;
    }
    HttpException httpException = (HttpException) e;
    ResponseBody body = httpException.response().errorBody();
    if (body == null)
        return defaultMsg;
    try {
        String errorMessage = getErrorMessage(context, httpException, body.string());
        return errorMessage == null ? defaultMsg : errorMessage;
    } catch (IOException e1) {
        e1.printStackTrace();
        return defaultMsg;
    }
}
Also used : HttpException(com.jakewharton.retrofit2.adapter.rxjava2.HttpException) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody)

Aggregations

HttpException (retrofit2.HttpException)29 JSONException (org.json.JSONException)12 JsonParseException (com.google.gson.JsonParseException)11 ConnectException (java.net.ConnectException)10 ParseException (android.net.ParseException)8 SocketTimeoutException (java.net.SocketTimeoutException)7 UnknownHostException (java.net.UnknownHostException)7 Disposable (io.reactivex.disposables.Disposable)6 IOException (java.io.IOException)6 Response (retrofit2.Response)5 TextUtils (android.text.TextUtils)4 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)4 Test (org.junit.Test)4 NonNull (android.support.annotation.NonNull)3 Gson (com.google.gson.Gson)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 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)3