简体   繁体   English

尝试使用RxJava和Kotlin从没有Wifi连接的Internet上获取数据时出错

[英]Error while trying to fetch data from Internet without Wifi connection using RxJava and Kotlin

Hello guys I have in my BaseActivity the following function. 大家好,我在BaseActivity中具有以下功能。

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

And I handle the error in the observer's onNext() if the server returns a response, but the problem when there's no Internet connection on the device at all!! 如果服务器返回响应,我将处理观察者的onNext()中的错误,但是当设备上根本没有Internet连接时就会出现问题! It throws the following exception 它引发以下异常

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)

And this is the usage of the previously mentioned function 这是前面提到的功能的用法

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

That problem is connected with the way you subscribing to observable. 这个问题与您订阅的可观察方式有关。 According to documentation when using subscribe() without passing action for handling errors, you should receive OnErrorNotImplementedException when source throws exceptions - that's because default exception handler from RxJavaPlugins is used. 根据文档,在使用subscribe()而不传递错误处理措施的情况下,当源引发异常时,您应该收到OnErrorNotImplementedException这是因为使用了RxJavaPlugins的默认异常处理程序。

To resolve that problem use one of overloaded subscribe methods with onError parameter. 若要解决该问题,请使用带有onError参数的重载subscribe方法之一。 For example, public final Disposable subscribe(Consumer onNext, Consumer onError) 例如, 公共最终Disposable订阅(Consumer onNext,Consumer onError)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM