[英]Resubscribing an observable if timeout exception occurs
i'm using retrofit to return an observable as a result of REST API call to server. 我正在使用改造,以通过对服务器的REST API调用返回可观察到的结果。 Its very usual that a request timeout exception occurs and observable stops executing. 通常会发生请求超时异常,并且可观察到的停止执行。 How to resubscribe of retry if exception is of a specific type 如果异常是特定类型,如何重新申请重试
myObservable
.subscribe(new Subscriber<Something> sub(){
@override
void onNext(Something something){
//do something with something
}
@override
void onError(Throwable e){
//retry and resend call to server if e is request timeout exception
}
You can use the retry operator. 您可以使用重试运算符。
Example: 例:
myObservable
.retry((retryCount, throwable) -> retryCount < 3 && throwable instanceof SocketTimeoutException)
.subscribe(new Subscriber<Something> sub(){
@override
void onNext(Something something){
//do something with something
}
@override
void onError(Throwable e){
}
In the example it will resubscribe when there is a SocketTimeoutException
max 3 times. 在该示例中,当SocketTimeoutException
最多出现3次时,它将重新订阅。
or without lambda: 或不带lambda:
myObservable
.retry(new Func2<Integer, Throwable, Boolean>() {
@Override
public Boolean call(Integer retryCount, Throwable throwable) {
return retryCount < 3 && throwable instanceof SocketTimeoutException;
}
})
.subscribe(new Subscriber<Something> sub(){
@override
void onNext(Something something){
//do something with something
}
@override
void onError(Throwable e){
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.