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了,最近开始使用,发现上面这么一个异常,以前用没有遇到过,网上很少有关于这个bug的解决方法,
经过查询加上直觉,最后解决,现在记录下解决的过程
最开始报了一个其他的错,如下:
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必须以“/”结尾,只要是这样的都可以,不一定只有域名地址“https://test-api.ddd.com/”
修改后接着运行就本文的异常“io.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","");
}
});
再看下修复后的代码:
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();
}
});
哈哈,有发现区别吗?
其实就是调用.subscribe()一个参数的改成了2个参数,看下源码:
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参数,是不是和
“io.reactivex.exceptions.OnErrorNotImplementedException: HTTP 404 Not Found”异常名称吻合了呢,
跟踪下第一个方法,第一个方法是默认帮传了一个参数:
publicstaticfinalConsumer ON_ERROR_MISSING =newConsumer() {
@Override
publicvoidaccept(Throwable error){
RxJavaPlugins.onError(newOnErrorNotImplementedException(error));
}
};
看到了吧,是不是很面熟啊,哈哈,当用这个方法,一旦出现异常就会抛出去,上一层就是我们处理,而我们并未处理,
于是就crash, 所以我们最好用2个参数的,有了异常抛给我们,我们可以打印等处理,这样我们知道异常且不会造成crash。
接下来就是处理网络请求的问题了,这里我是遇到了404,那是因为get弄成了post,改下就OK啦。