【发布时间】:2017-05-15 04:17:50
【问题描述】:
我有一个从改造中生成的 observable,我正在尝试实现错误处理,特别是连接超时。出错的订阅者被调用得很好,但应用程序仍然因套接字超时错误而崩溃。有什么建议吗?
Observable<History> history = api.returnHistoryRX(pair, String.valueOf(unixTime-3600), String.valueOf(unixTime));
history.onErrorReturn(throwable -> null);
订阅者
public void getPriceNow(Observable<List<history>> history, String pair) {
Timestamp timestamp2;
timestamp2 = new Timestamp(System.currentTimeMillis());
history.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(history1 -> {
String currentValue;
if (history1.size()>0){
System.out.println("testing rx");
}
}, e->System.out.println("getPriceNow: error called"));
}
为了测试,我使用 okhttp 将超时设置为不合理的低
private OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.MILLISECONDS)
.readTimeout(30L, TimeUnit.MILLISECONDS)
.writeTimeout(100L, TimeUnit.MILLISECONDS);
错误链如下所示:
java.lang.IllegalStateException:在 Scheduler.Worker 线程上抛出异常。添加onError处理。
原因:rx.exceptions.OnErrorNotImplementedException:连接失败
原因:java.net.SocketTimeoutException: 连接失败
【问题讨论】:
标签: android error-handling rx-java retrofit2 rx-android