我使用Retrofit和RxJava2实现了一次调用,但是只有当你得到一个与404不同的代码时才需要它重试。重试404是没有意义的。这就是我正在使用的
new RequestFactory()
.requestBuilder
.create(Service.class)
.getData(id)
.map(response -> response.object)
.doOnError(t -> Log.e(NET, "Error fetching data id '" + id + "': " + t))
.retry(3)
.onErrorResumeNext(Observable.empty())
.subscribeOn(Schedulers.io())
答案 0 :(得分:2)
You can use the other form of retry()
to conditionally retry.
...
.retryWhen( error -> error.flatMap( responseType -> checkResponseType( responseType ) ) )
...
and then
Observable<Boolean> checkResponseType( ResponseException response ) {
if ( response.getCode() == 404 ) {
return Observable.error( response );
}
return Observable.just( Boolean.TRUE );
}
This will monitor the error response you get and check for the 404 value. If it is a 404, it won't retry, otherwise it will.
答案 1 :(得分:0)
感谢Bob,我设法使用他的响应和这个(Error code from Throwable - Android)。这是使用Throwable的最终解决方案。我删除了
.retry(3)
我补充了鲍勃的建议。并且被调用的函数被替换为使用Throwable而不是ResponseException
private static Observable<Boolean> checkResponseType( Throwable response ) {
if ((response instanceof HttpException) && ((HttpException) response).code() == 404) {
return Observable.error(response);
}
return Observable.just(Boolean.TRUE);
}