首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用RxJava和Kotlin在没有Wifi连接的情况下尝试从互联网获取数据时出错

使用RxJava和Kotlin在没有Wifi连接的情况下尝试从互联网获取数据时出错
EN

Stack Overflow用户
提问于 2018-09-18 18:25:01
回答 1查看 65关注 0票数 1

大家好,我的BaseActivity中有以下函数。

代码语言:javascript
复制
override fun <T> subscribeToInternet(observable: Observable<Response<BaseResponse<T>>>, observer: Observer<BaseResponse<T>>) {
    observable.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .doOnSubscribe { observer.onSubscribe(it) }
            .doOnError {
                Log.d(TAG, it.message)
                observer.onError(it)
            }
            .doOnComplete { observer.onComplete() }
            .doOnNext {
                Log.d(TAG, "${it.body() ?: "no body"}")
                Log.d(TAG, "${it.errorBody()?.string() ?: "no error body"}")
                Log.d(TAG, it.code().toString())
                when {
                    it.code() == 401 -> {
                        view.userUnauthenticated()
                        observer.onNext(BaseResponse(false, "unauthenticated", null))
                        Log.d(TAG, "UNAUTHENTICATED")
                    }
                    it.code() == 423 -> {
                        view.userBlocked()
                        observer.onNext(BaseResponse(false, "blocked", null))
                        Log.d(TAG, "BLOCKED")
                    }
                    it.isSuccessful -> observer.onNext(it.body()!!)
                    it.code() == 429 -> observer.onNext(BaseResponse(false, "Too many attempts", null))
                    it.code() == 400 -> observer.onNext(BaseResponse(false, "Invalid Email or password", null))
                    else -> observer.onNext(BaseResponse(false, "", null))
                }
            }
            .subscribe()
}

如果服务器返回响应,我会处理观察者的onNext()中的错误,但当设备上根本没有互联网连接时就会出现问题!!它抛出以下异常

代码语言:javascript
复制
at io.reactivex.internal.operators.observable.ObservableDoOnEach$DoOnEachObserver.onError(ObservableDoOnEach.java:119)
    at io.reactivex.internal.observers.DisposableLambdaObserver.onError(DisposableLambdaObserver.java:64)
    at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.checkTerminated(ObservableObserveOn.java:276)
    at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.drainNormal(ObservableObserveOn.java:172)
    at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.run(ObservableObserveOn.java:252)

这是前面提到的函数的用法

代码语言:javascript
复制
override fun sendLoginRequest(email: String, password: String, fcm_token: String) {
    subscribeToInternet(dataManager.sendLoginRequest(email, password, fcm_token), this)
}

override fun onComplete() {

}

override fun onSubscribe(d: Disposable) {
    DisposableManager.add(d)
}

override fun onNext(t: BaseResponse<LoginData>) {
    if(t.status) {
        Log.d(TAG, "${t.data}")
        dataManager.createLoginSession(t.data!!)
        view.loginSuccess()
    } else {
        Log.d(TAG, t.message)
        view.showError(t.message)
    }
}

override fun onError(e: Throwable) {
    view.showToastError()
    Log.d(TAG, e.message)
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-19 04:43:31

这个问题与你订阅observable的方式有关。根据文档,在不传递处理错误的操作的情况下使用subscribe()时,当源代码抛出异常时应该收到OnErrorNotImplementedException -这是因为使用了来自RxJavaPlugins的默认异常处理程序。

要解决这个问题,请使用带有onError参数的重载subscribe方法之一。例如,public final Disposable subscribe(Consumer onNext, Consumer onError)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52384443

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档