我正在尝试从我的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)
答案 0 :(得分:1)
如果发生该错误,您可能想检查BASE_URL
否以确保没有https
还要确保打印您的方法给出的错误。您应该可以从onError
方法中这样调用它:
override fun onError(e: Throwable) {
e.printStackTrace()
}