OnError在观察改装时可观察到的

时间:2019-07-31 07:39:41

标签: java android kotlin rx-java dagger-2

我正在尝试从我的String中观察一个API,但是我不知道为什么我订阅它时总是将它跳到onError方法很好地改造Builder。

我的NetworkModule

@Module
class NetworkModule{

    /**
     * Provides the Post service implementation.
     * @param retrofit the Retrofit object used to instantiate the service
     * @return the Post service implementation.
     */
    @Provides
    fun provideUserAuth(): ApiSMS{
        return Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build()
            .create(ApiSMS::class.java)
    }
}

我的ApiCall

interface ApiSMS {

    @get:POST("/api/auth/sign_in")
    val getAuthentication: Observable<Credentials>

    @get:GET("/api/status")
    val getStatus: Observable<String>
}

我从那里观察到的ViewModel。当我打电话给getStatus时,创建了我的Retrofit实例,但只在onSubscribe然后是onError上进行了

//Get Api Status
fun getStatus() {
    apiSMS.getStatus
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(getObserver())
}

private fun getObserver(): Observer<String> {
    return object : Observer<String> {
        override fun onComplete() {
            Log.d("test", "onComplete")
        }

        override fun onSubscribe(d: Disposable) {
            Log.d("test", "onSubscribe")
            disposable = d
        }

        override fun onNext(t: String) {
            Log.d("test", "onNext")
            Log.d("test", t)
        }

        override fun onError(e: Throwable) {
            Log.d("test", "onError")
        }
    }
}

onError堆栈跟踪:

2019-07-31 09:48:59.911 12063-12063/es.devinet.eptv W/System.err: java.net.UnknownServiceException: CLEARTEXT communication to (MY URL PRIVATE) not permitted by network security policy
2019-07-31 09:48:59.912 12063-12063/es.devinet.eptv W/System.err:     at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:147)
2019-07-31 09:48:59.912 12063-12063/es.devinet.eptv W/System.err:     at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:257)
2019-07-31 09:48:59.912 12063-12063/es.devinet.eptv W/System.err:     at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)
2019-07-31 09:48:59.912 12063-12063/es.devinet.eptv W/System.err:     at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)
2019-07-31 09:48:59.912 12063-12063/es.devinet.eptv W/System.err:     at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
2019-07-31 09:48:59.912 12063-12063/es.devinet.eptv W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
2019-07-31 09:48:59.912 12063-12063/es.devinet.eptv W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
2019-07-31 09:48:59.912 12063-12063/es.devinet.eptv W/System.err:     at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
2019-07-31 09:48:59.912 12063-12063/es.devinet.eptv W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
2019-07-31 09:48:59.912 12063-12063/es.devinet.eptv W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
2019-07-31 09:48:59.912 12063-12063/es.devinet.eptv W/System.err:     at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
2019-07-31 09:48:59.912 12063-12063/es.devinet.eptv W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
2019-07-31 09:48:59.913 12063-12063/es.devinet.eptv W/System.err:     at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
2019-07-31 09:48:59.913 12063-12063/es.devinet.eptv W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
2019-07-31 09:48:59.913 12063-12063/es.devinet.eptv W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
2019-07-31 09:48:59.913 12063-12063/es.devinet.eptv W/System.err:     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:254)
2019-07-31 09:48:59.913 12063-12063/es.devinet.eptv W/System.err:     at okhttp3.RealCall.execute(RealCall.java:92)
2019-07-31 09:48:59.913 12063-12063/es.devinet.eptv W/System.err:     at retrofit2.OkHttpCall.execute(OkHttpCall.java:186)
2019-07-31 09:48:59.913 12063-12063/es.devinet.eptv W/System.err:     at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:45)
2019-07-31 09:48:59.913 12063-12063/es.devinet.eptv W/System.err:     at io.reactivex.Observable.subscribe(Observable.java:12267)
2019-07-31 09:48:59.913 12063-12063/es.devinet.eptv W/System.err:     at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)
2019-07-31 09:48:59.913 12063-12063/es.devinet.eptv W/System.err:     at io.reactivex.Observable.subscribe(Observable.java:12267)
2019-07-31 09:48:59.913 12063-12063/es.devinet.eptv W/System.err:     at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
2019-07-31 09:48:59.913 12063-12063/es.devinet.eptv W/System.err:     at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:578)
2019-07-31 09:48:59.913 12063-12063/es.devinet.eptv W/System.err:     at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
2019-07-31 09:48:59.913 12063-12063/es.devinet.eptv W/System.err:     at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
2019-07-31 09:48:59.914 12063-12063/es.devinet.eptv W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2019-07-31 09:48:59.914 12063-12063/es.devinet.eptv W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
2019-07-31 09:48:59.914 12063-12063/es.devinet.eptv W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2019-07-31 09:48:59.914 12063-12063/es.devinet.eptv W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2019-07-31 09:48:59.914 12063-12063/es.devinet.eptv W/System.err:     at java.lang.Thread.run(Thread.java:764)

1 个答案:

答案 0 :(得分:1)

如果发生该错误,您可能想检查BASE_URL 以确保没有https

还要确保打印您的方法给出的错误。您应该可以从onError方法中这样调用它:

override fun onError(e: Throwable) {
           e.printStackTrace()
        }