io.reactivex.exceptions.OnErrorNotImplementedException: HTTP 404 Not Found
? ? ? ? at io.reactivex.internal.functions.Functions$14.accept(Functions.java:229)
? ? ? ? at io.reactivex.internal.functions.Functions$14.accept(Functions.java:226)
? ? ? ? at io.reactivex.internal.subscribers.LambdaSubscriber.onError(LambdaSubscriber.java:76)
? ? ? ? at io.reactivex.internal.operators.flowable.FlowableDoOnEach$DoOnEachSubscriber.onError(FlowableDoOnEach.java:110)
? ? ? ? at io.reactivex.internal.operators.flowable.FlowableObserveOn$BaseObserveOnSubscriber.checkTerminated(FlowableObserveOn.java:207)
? ? ? ? at io.reactivex.internal.operators.flowable.FlowableObserveOn$ObserveOnSubscriber.runAsync(FlowableObserveOn.java:392)
? ? ? ? at io.reactivex.internal.operators.flowable.FlowableObserveOn$BaseObserveOnSubscriber.run(FlowableObserveOn.java:176)
? ? ? ? at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:109)
? ? ? ? at android.os.Handler.handleCallback(Handler.java:743)
? ? ? ? at android.os.Handler.dispatchMessage(Handler.java:95)
? ? ? ? at android.os.Looper.loop(Looper.java:150)
? ? ? ? at android.app.ActivityThread.main(ActivityThread.java:5621)
? ? ? ? at java.lang.reflect.Method.invoke(Native Method)
? ? ? ? at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
? ? ? ? at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
?? ? Caused by: retrofit2.adapter.rxjava2.HttpException: HTTP 404 Not Found
? ? ? ? at retrofit2.adapter.rxjava2.BodyObservable$BodyObserver.onNext(BodyObservable.java:54)
? ? ? ? at retrofit2.adapter.rxjava2.BodyObservable$BodyObserver.onNext(BodyObservable.java:37)
? ? ? ? at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:44)
? ? ? ? at io.reactivex.Observable.subscribe(Observable.java:10700)
? ? ? ? at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)
? ? ? ? at io.reactivex.Observable.subscribe(Observable.java:10700)
? ? ? ? at io.reactivex.internal.operators.observable.ObservableSubscribeOn$1.run(ObservableSubscribeOn.java:39)
? ? ? ? at io.reactivex.Scheduler$1.run(Scheduler.java:138)
? ? ? ? at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:59)
? ? ? ? at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:51)
? ? ? ? at java.util.concurrent.FutureTask.run(FutureTask.java:237)
? ? ? ? at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:154)
? ? ? ? at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)
? ? ? ? at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
? ? ? ? at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
? ? ? ? at java.lang.Thread.run(Thread.java:833)
好久不用retrofit了,最近開始使用,發(fā)現(xiàn)上面這么一個異常,以前用沒有遇到過,網(wǎng)上很少有關(guān)于這個bug的解決方法,
經(jīng)過查詢加上直覺,最后解決,現(xiàn)在記錄下解決的過程
最開始報了一個其他的錯,如下:
java.lang.IllegalArgumentException: baseUrl must end in /: https://test-api.ddd.com/api/v1
以前也沒遇到過,這個異常倒是可以搜索到很多答案
retrofit =newRetrofit.Builder()
? ? ? ? ? ? ? ? .baseUrl(APIConfig.getAPIHost())
? ? ? ? ? ? ? ? .client(client)
? ? ? ? ? ? ? ? .addConverterFactory(GsonConverterFactory.create(GSON))
? ? ? ? ? ? ? ? .addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
? ? ? ? ? ? ? ? .build();
.baseUrl("https://test-api.ddd.com/api/v1/")
host必須以“/”結(jié)尾,只要是這樣的都可以,不一定只有域名地址“https://test-api.ddd.com/”
修改后接著運行就本文的異?!癷o.reactivex.exceptions.OnErrorNotImplementedException: HTTP 404 Not Found”,app
crash, 為了解決這個bug, 搞了一下午,看描述大概是說rxjava報了一個錯,我們沒有處理
先看下報錯時的代碼:
DefaultRetrofit.api().fetchRoomList(1,100)
? ? ? ? ? ? ? ? .observeOn(AndroidSchedulers.mainThread())
.subscribe(newConsumer>() {
@Override
publicvoidaccept(@NonNullArrayListResult result){
Log.d("Alex","");
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
再看下修復(fù)后的代碼:
DefaultRetrofit.api().fetchRoomList(1,100)
? ? ? ? ? ? ? ? .observeOn(AndroidSchedulers.mainThread())
.subscribe(newConsumer>() {
@Override
publicvoidaccept(@NonNullArrayListResult result){
Log.d("Alex","");
? ? ? ? ? ? ? ? ? ? }
},newConsumer() {
@Override
publicvoidaccept(Throwable throwable)throwsException{
? ? ? ? ? ? ? ? ? ? ? ? throwable.printStackTrace();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
哈哈,有發(fā)現(xiàn)區(qū)別嗎?
其實就是調(diào)用.subscribe()一個參數(shù)的改成了2個參數(shù),看下源碼:
publicabstractclassFlowableimplementsPublisher{
publicfinalDisposablesubscribe(Consumer onNext){
returnsubscribe(onNext, Functions.ON_ERROR_MISSING,
? ? ? ? ? ? ? ? Functions.EMPTY_ACTION, FlowableInternalHelper.RequestMax.INSTANCE);
? ? }
publicfinalDisposablesubscribe(Consumer onNext, Consumer onError){
returnsubscribe(onNext, onError, Functions.EMPTY_ACTION, FlowableInternalHelper.RequestMax.INSTANCE);
? ? }
}
Flowable是rxjava2.0以后才有的,這里第二方法有個onError參數(shù),是不是和
“io.reactivex.exceptions.OnErrorNotImplementedException: HTTP 404 Not Found”異常名稱吻合了呢,
跟蹤下第一個方法,第一個方法是默認幫傳了一個參數(shù):
publicstaticfinalConsumer ON_ERROR_MISSING =newConsumer() {
@Override
publicvoidaccept(Throwable error){
RxJavaPlugins.onError(newOnErrorNotImplementedException(error));
? ? ? ? }
? ? };
看到了吧,是不是很面熟啊,哈哈,當(dāng)用這個方法,一旦出現(xiàn)異常就會拋出去,上一層就是我們處理,而我們并未處理,
于是就crash, ?所以我們最好用2個參數(shù)的,有了異常拋給我們,我們可以打印等處理,這樣我們知道異常且不會造成crash。
接下來就是處理網(wǎng)絡(luò)請求的問題了,這里我是遇到了404,那是因為get弄成了post,改下就OK啦。