簡體   English   中英

OnError在觀察改裝時可觀察到的

[英]OnError at observe an Observable on Retrofit

我正在嘗試從我的API觀察一個String ,但是我不知道為什么即使我很好地創建了Retrofit Builder,當我訂閱它時,它總是一直跳轉到onError方法。

我的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)

如果發生此錯誤,您可能要檢查BASE_URL 並非沒有https

還要確保打印您的方法給出的錯誤。 您應該能夠從onError方法中這樣調用它:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM