我已经添加了onErrorReturn和doOnError,但我仍然会遇到致命的错误.这是我的代码:
apiInterface.submitDataToAnalyze("dataToAnalyze", HelperFunctions.authenticationData, 1, uuid, dataToAnalyze.toString(), todayDate) .onErrorReturn(new Func1() { @Override public BasicResponse call(Throwable throwable) { Log.e(FILE_NAME, "submitDataToAnalyze throwable??"); Log.e(FILE_NAME, throwable.getMessage().toString()); return null; } }) .flatMap(new Func1 >() { @Override public Observable call(BasicResponse basicResponse) { Log.v(FILE_NAME, "basicResponse: " + basicResponse.toString()); if (basicResponse.getResult() == 1) { //update TABLE_REQUEST_SUGGESTIONS with serverResponse = 1 dbc.updateRequestSuggestionLog(strTodayDate, strDataToAnalyze, basicResponse.getResult()); return apiInterface.getSuggestion("getSuggestion", HelperFunctions.authenticationData, HelperFunctions.requestVersion, uuid, strLocationLat, strLocationLng, radius, requestForDate); } else { return Observable.just(null); } } }).doOnError(new Action1 () { @Override public void call(Throwable t) { Log.e(FILE_NAME, "masuk doOnError??"); Log.e(FILE_NAME, t.getMessage().toString()); } }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(); }
具体来说,这是错误消息:
E/ISRS: masuk doOnError?? E/ISRS: masuk 22?? E/AndroidRuntime: FATAL EXCEPTION: main java.lang.IllegalStateException: Exception thrown on Scheduler.Worker thread. Add `onError` handling. at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:60)
附加信息:如果submitDataToAnalyze()由于连接错误而连接到我的服务器时出现问题,则会发生致命错误
在onErrorReturn
方法中,您返回null,然后此null沿着链进一步传播.因此它会导致NPE(例如basicResponse.toString()
).在这种情况下doOnError
被调用,但您的订阅者仍然是Throwable,保持未处理状态.
所以你必须通过实现传递Subscriber
实例onError
.
另一个选择:在你的链中你可以使用类似的东西onErrorResumeNext(Observable.empty())
,所以它什么都不会发出然后完成.在这种情况下,您不必将订阅服务器实现传递给subscribe
.