如何在Android中处理OkHttp拦截器中的IOException?

如何在Android中处理OkHttp拦截器中的IOException?,android,retrofit,retrofit2,rx-java2,okhttp,Android,Retrofit,Retrofit2,Rx Java2,Okhttp,我有一个自定义拦截器将API密钥附加到永久API请求。代码如下: 公共类HeaderInterceptor实现拦截器{ 私有最终ProfileDbDataSource profileDbRepository; @注入公共HeaderInterceptor(ProfileDbDataSource profileDbRepository){ this.profileDbRepository=profileDbRepository; } @NonNull@Override public Respon

我有一个自定义拦截器将API密钥附加到永久API请求。代码如下:

公共类HeaderInterceptor实现拦截器{
私有最终ProfileDbDataSource profileDbRepository;
@注入公共HeaderInterceptor(ProfileDbDataSource profileDbRepository){
this.profileDbRepository=profileDbRepository;
}
@NonNull@Override public Response intercept(@NonNull Chain-Chain)引发IOException{
如果(!chain.request().url().toString().contains(Constants.GET\u API\u KEY)){
Request.Builder requestBuilder=attachApiKeyAsQueryParam(链);
返回链。继续(requestBuilder.build());
}
返回chain.procedue(chain.request());
}
私有请求。生成器attachApiKeyAsQueryParam(链){
字符串apiKey=profileDbRepository.getLoggedInProfile().blockingGet().getApiKey();
Request original=chain.Request();
HttpUrl originalHttpUrl=original.url();
HttpUrl=originalHttpUrl.newBuilder()
.addQueryParameter(常量.USER\u API\u键,apiKey)
.build();
返回原始的.newBuilder().url(url);
}

}
这不是拦截器的问题,而是如何使用网络请求的接收流的问题。IOException通常是预期的错误,在执行I/O时,您应该准备好捕获并处理它们。 要使用Rx Observable执行此操作,请在调用
subscribe
时传递
onError
参数的错误处理程序:

apiClient.request()
    .subscribe(
        /* onNext */ (response) -> /* handle response */,
        /* onError */ (error) -> /* handle error */
    )

是的,看起来你是对的,但奇怪的是,我没有任何没有onError()的RxJava链,因为我有RxLint。我有几个可观察的.merge和.zip()。这可能是一个原因吗?你有什么想法吗?那我不确定。。。您是否可能在某个地方重新引用异常或在OneError回调中使用Exception.propagate()?在这种情况下,merge()和zip()不应该相关。如果你找不到任何东西,我会尝试追踪到底是哪个网络呼叫导致了崩溃,并检查它们是否有问题。检查一下启动程序: