简体   繁体   English

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

[英]OnError at observe an Observable on Retrofit

I'm trying to observe a String from my API but i don't know why it jumps all time to onError method when i subscribe to it even though it creates my Retrofit Builder well. 我正在尝试从我的API观察一个String ,但是我不知道为什么即使我很好地创建了Retrofit Builder,当我订阅它时,它总是一直跳转到onError方法。

My NetworkModule : 我的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)
    }
}

My ApiCall : 我的ApiCall

interface ApiSMS {

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

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

My ViewModel from where i observe. 我从那里观察的ViewModel When i call getStatus creates my Retrofit instance but only goes on the onSubscribe and then onError : 当我打电话给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")
        }
    }
}

The onError stack trace: 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)

In case of that error you might wanna check your BASE_URL NOT to be without https 如果发生此错误,您可能要检查BASE_URL 并非没有https

Also make sure to print the error your method is giving. 还要确保打印您的方法给出的错误。 You should be able to call it like this from the onError method: 您应该能够从onError方法中这样调用它:

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

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

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