【发布时间】:2014-10-02 11:56:39
【问题描述】:
当使用带有 Retrofit 的 Observables 时,你如何处理网络故障?
鉴于此代码:
Observable<GetJobResponse> observable = api.getApiService().getMyData();
observable
.doOnNext(new Action1<GetJobResponse>() {
@Override
public void call(GetJobResponse getJobResponse) {
//do stuff with my data
}
})
.doOnError(new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
//do stuff with error message
}
});
请求在没有网络的情况下失败,并且不会调用 onError。它不会崩溃,而是默默地失败。日志显示 Retrofit 收到错误:
java.net.UnknownHostException: Unable to resolve host "api-staging.sittercity.com": No address associated with hostname
at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at com.squareup.okhttp.internal.Dns$1.getAllByName(Dns.java:29)
使用回调将其简单地传递给 onFailure(RetrofitError 错误)。我怎样才能用 RxJava 实现这一点?
【问题讨论】:
标签: android observable retrofit rx-java