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

问题描述 投票:0回答:2

我有一个自定义拦截器,可以将 API 密钥附加到所有 API 请求中。这是代码:

public class HeaderInterceptor implements Interceptor {

  private final ProfileDbDataSource profileDbRepository;

  @Inject public HeaderInterceptor(ProfileDbDataSource profileDbRepository) {
    this.profileDbRepository = profileDbRepository;
  }

  @NonNull @Override public Response intercept(@NonNull Chain chain) throws IOException {
    if (!chain.request().url().toString().contains(Constants.GET_API_KEY)) {
      Request.Builder requestBuilder = attachApiKeyAsQueryParam(chain);
      return chain.proceed(requestBuilder.build());
    }
    return chain.proceed(chain.request());
  }

  private Request.Builder attachApiKeyAsQueryParam(Chain chain) {
    String apiKey = profileDbRepository.getLoggedInProfile().blockingGet().getApiKey();
    Request original = chain.request();
    HttpUrl originalHttpUrl = original.url();
    HttpUrl url = originalHttpUrl.newBuilder()
      .addQueryParameter(Constants.USER_API_KEY, apiKey)
      .build();
    return original.newBuilder().url(url);
  }
}

我看到

return chain.proceed(requestBuilder.build());
行发生了一些崩溃,所有这些异常都是
IOException
的子类。

我见过

SocketTimeoutException
UnknownHostException
ConnectException

这是一个堆栈跟踪:

io.reactivex.exceptions.UndeliverableException: 
  at io.reactivex.plugins.RxJavaPlugins.onError (RxJavaPlugins.java:367)
  at io.reactivex.internal.operators.observable.ObservableFlatMapSingle$FlatMapSingleObserver.innerError (ObservableFlatMapSingle.java:204)
  at io.reactivex.internal.operators.observable.ObservableFlatMapSingle$FlatMapSingleObserver$InnerObserver.onError (ObservableFlatMapSingle.java:289)
  at io.reactivex.internal.operators.single.SingleFlatMap$SingleFlatMapCallback.onError (SingleFlatMap.java:90)
  at io.reactivex.internal.operators.single.SingleSubscribeOn$SubscribeOnObserver.onError (SingleSubscribeOn.java:73)
  at io.reactivex.internal.operators.observable.ObservableSingleSingle$SingleElementObserver.onError (ObservableSingleSingle.java:93)
  at retrofit2.adapter.rxjava2.BodyObservable$BodyObserver.onError (BodyObservable.java:72)
  at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual (CallExecuteObservable.java:59)
  at io.reactivex.Observable.subscribe (Observable.java:12284)
  at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual (BodyObservable.java:34)
  at io.reactivex.Observable.subscribe (Observable.java:12284)
  at io.reactivex.internal.operators.observable.ObservableSingleSingle.subscribeActual (ObservableSingleSingle.java:35)
  at io.reactivex.Single.subscribe (Single.java:3666)
  at io.reactivex.internal.operators.single.SingleSubscribeOn$SubscribeOnObserver.run (SingleSubscribeOn.java:89)
  at io.reactivex.Scheduler$DisposeTask.run (Scheduler.java:608)
  at io.reactivex.internal.schedulers.ScheduledRunnable.run (ScheduledRunnable.java:66)
  at io.reactivex.internal.schedulers.ScheduledRunnable.call (ScheduledRunnable.java:57)
  at java.util.concurrent.FutureTask.run (FutureTask.java:266)
  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run (ScheduledThreadPoolExecutor.java:301)
  at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1167)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:641)
  at java.lang.Thread.run (Thread.java:923)
Caused by: java.net.SocketTimeoutException: 
  at okhttp3.internal.http2.Http2Stream$StreamTimeout.newTimeoutException (Http2Stream.kt:677)
  at okhttp3.internal.http2.Http2Stream$StreamTimeout.exitAndThrowIfTimedOut (Http2Stream.kt:686)
  at okhttp3.internal.http2.Http2Stream.takeHeaders (Http2Stream.kt:143)
  at okhttp3.internal.http2.Http2ExchangeCodec.readResponseHeaders (Http2ExchangeCodec.kt:96)
  at okhttp3.internal.connection.Exchange.readResponseHeaders (Exchange.kt:106)
  at okhttp3.internal.http.CallServerInterceptor.intercept (CallServerInterceptor.kt:79)
  at okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109)
  at okhttp3.logging.HttpLoggingInterceptor.intercept (HttpLoggingInterceptor.kt:221)
  at okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109)
  at okhttp3.internal.connection.ConnectInterceptor.intercept (ConnectInterceptor.kt:34)
  at okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109)
  at okhttp3.internal.cache.CacheInterceptor.intercept (CacheInterceptor.kt:95)
  at okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109)
  at okhttp3.internal.http.BridgeInterceptor.intercept (BridgeInterceptor.kt:83)
  at okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109)
  at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept (RetryAndFollowUpInterceptor.kt:76)
  at okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109)
  at com.anstar.data.core.HeaderInterceptor.intercept (HeaderInterceptor.java:24)
  at okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.kt:109)
  at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp (RealCall.kt:201)
  at okhttp3.internal.connection.RealCall.execute (RealCall.kt:154)
  at retrofit2.OkHttpCall.execute (OkHttpCall.java:204)
  at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual (CallExecuteObservable.java:45)

android retrofit retrofit2 rx-java2 okhttp
2个回答
0
投票

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

onError
时为
subscribe
参数传递错误处理程序:

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

0
投票

public class HeaderInterceptor implements Interceptor {

  private final ProfileDbDataSource profileDbRepository;

  @Inject public HeaderInterceptor(ProfileDbDataSource profileDbRepository) {
    this.profileDbRepository = profileDbRepository;
  }

  @NonNull @Override public Response intercept(@NonNull Chain chain) throws IOException {
    if (!chain.request().url().toString().contains(Constants.GET_API_KEY)) {
      Request.Builder requestBuilder = attachApiKeyAsQueryParam(chain);
      return chain.proceed(requestBuilder.build());
    }
    return chain.proceed(chain.request());
  }

  private Request.Builder attachApiKeyAsQueryParam(Chain chain) {
    String apiKey = profileDbRepository.getLoggedInProfile().blockingGet().getApiKey();
    Request original = chain.request();
    HttpUrl originalHttpUrl = original.url();
    HttpUrl url = originalHttpUrl.newBuilder()
      .addQueryParameter(Constants.USER_API_KEY, apiKey)
      .build();
    return original.newBuilder().url(url);
  }
}

© www.soinside.com 2019 - 2024. All rights reserved.