OnError at observe 一个 Observable on Retrofit
OnError at observe an Observable on Retrofit
我试图从我的 API
观察一个 String
但我不知道为什么当我订阅它时它总是跳到 onError
方法,即使它很好地创建了我的 Retrofit 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)
如果出现该错误,您可能需要检查 BASE_URL
NOT 是否没有 https
还要确保打印出您的方法给出的错误。您应该可以从 onError
方法中这样调用它:
override fun onError(e: Throwable) {
e.printStackTrace()
}
我试图从我的 API
观察一个 String
但我不知道为什么当我订阅它时它总是跳到 onError
方法,即使它很好地创建了我的 Retrofit 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)
如果出现该错误,您可能需要检查 BASE_URL
NOT 是否没有 https
还要确保打印出您的方法给出的错误。您应该可以从 onError
方法中这样调用它:
override fun onError(e: Throwable) {
e.printStackTrace()
}