小编Abh*_*ari的帖子

在使用RxJava2和Retrofit在android中实现即时搜索时获取java.io.InterruptedIOException

因此,我尝试使用rxjava2和改造来实现即时搜索,该过程很简单,只要用户更改了文本publish.onNext()(发布是一个PublishSubject对象)就可以了。我添加了filter和debounce以及switch映射运算符,以在文本的长度大于阈值并且不会同时使用连续输入进行调用时方便从服务器进行搜索。

这是代码:

subject = PublishSubject.create();
    getCompositeDisposable().add(subject
            .filter(s -> s.length() >= 3)
            .debounce(300,
                    TimeUnit.MILLISECONDS)
            .switchMap(s -> getDataManager().getHosts(
                    getDataManager().getDeviceToken(),
                    s).observeOn(Schedulers.io()))
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .subscribe(hostResponses -> {
                getMvpView().hideEditLoading();
                if (hostResponses.size() != 0) {
                    if (this.hostResponses != null)
                        this.hostResponses.clear();
                    this.hostResponses = hostResponses;
                    getMvpView().setHostView(getHosts(hostResponses));
                } else {
                    getMvpView().onFieldError("No host found");
                }

            }, throwable -> {
                getMvpView().hideEditLoading();
                if (throwable instanceof HttpException) {
                   HttpException exception = (HttpException)throwable;
                    if (exception.code() == 401) {
                        getMvpView().onError(R.string.code_expired,
                                BaseUtils.TOKEN_EXPIRY_TAG);
                    }
                }

            })
    );
Run Code Online (Sandbox Code Playgroud)

现在我的代码可以正常工作,可以满足我的需要,但是当我输入一个长字符串并按退格按钮时,我遇到了一个错误,那就是当清除AutoCompleteTextView的文本时,会引发异常

这是异常的堆栈跟踪:

java.io.InterruptedIOException: thread interrupted  
at …
Run Code Online (Sandbox Code Playgroud)

java android retrofit2 rx-java2

1
推荐指数
1
解决办法
870
查看次数

标签 统计

android ×1

java ×1

retrofit2 ×1

rx-java2 ×1