我希望在我的Java应用程序中使用Observable包装经过身份验证的调用,该Observable会在发出请求之前根据需要重新授权我的用户。我使用Shape Code Blog中的这种方法在Objective-C中使用ReactiveCocoa做了类似的事情:
- (RACSignal *)doRequestAndRefreshTokenIfNecessary:(RACSignal *)requestSignal {
return [requestSignal catch:^(NSError *error) {
// Catch the error, refresh the token, and then do the request again.
BOOL hasRefreshToken = [UserManager sharedInstance].refreshToken != nil;
BOOL httpCode401AccessDenied = error.code == -1011;
if (httpCode401AccessDenied && hasRefreshToken) {
NSLog(@"Will attempt to refresh access token.");
return [[[self refreshToken] ignoreValues] concat:requestSignal];
}
return requestSignal;
}];
}
RACSignal是一个Observable的ReactiveCocoa模拟。此方法创建一个RACSignal,它将捕获初始请求发出的任何错误,如果该错误与给定条件匹配(在这种情况下,拒绝访问并且有可用的auth令牌),则会透明地触发新的RACSignal以使用刷新令牌然后重新发送原始信号。
RxJava是否提供类似的设施?我在Combining Observables文档上找不到与catch:
类似的内容。
答案 0 :(得分:2)
它看起来不像RxJava中存在的任何东西,但这似乎可以做到这一点(从ReactiveCocoa捕获的非常直接的翻译):
public class CatchObservable {
public interface CaughtErrorHandler {
public Observable<?> onError(Throwable throwable);
}
public static <T> Observable catchObserver(final Observable<T> observable, final CaughtErrorHandler errorHandler) {
return Observable.create(new Observable.OnSubscribeFunc<T>() {
@Override
public Subscription onSubscribe(final Observer<? super T> subscriber) {
return observable.subscribe(new Observer<T>() {
@Override
public void onCompleted() {
subscriber.onCompleted();
}
@Override
public void onError(Throwable e) {
Observable observable = errorHandler.onError(e);
observable.subscribe(subscriber);
}
@Override
public void onNext(T args) {
subscriber.onNext(args);
}
});
}
});
}
}
如此使用:
private Observable refreshAuthIfNecessary(final Observable<?> request) {
return CatchObservable.catchObserver(request, new CatchObservable.CaughtErrorHandler() {
@Override
public Observable<?> onError(Throwable throwable) {
return Observable.concat(WebServices.this.refreshAuth(), request);
}
});
}