Rxjava+Retrofit系列

網絡請求異常封裝

對於RxJava+Retrofit已經非常流行,博主也在公司項目中使用到了。其中的好處不用多說,今天就大致寫一下對Retrofit請求異常的封裝。

retrofit = new Retrofit.Builder()
        .baseUrl(BASE_URL)
        .client(HttpHelper.getInstance().getmOkHttpClient())
        .addCallAdapterFactory(RxErrorHandlingCallAdapterFactory.create())
        .addConverterFactory(GsonConverterFactory.create(new Gson()))
        .build();

這是對Retrofit簡單配置,這裏重點講一下addConverterFactory 的配置自定義一個call。

public class RxErrorHandlingCallAdapterFactory extends CallAdapter.Factory {

  private final RxJavaCallAdapterFactory original;

  private RxErrorHandlingCallAdapterFactory() {
    original = RxJavaCallAdapterFactory.create();
  }

  public static CallAdapter.Factory create() {
    return new RxErrorHandlingCallAdapterFactory();
  }

  @Override
  public CallAdapter<?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
    return new RxCallAdapterWrapper(retrofit, original.get(returnType, annotations, retrofit));
  }

  private static class RxCallAdapterWrapper implements CallAdapter<Observable<?>> {

    private final Retrofit retrofit;
    private final CallAdapter<?> wrapped;

    public RxCallAdapterWrapper(Retrofit retrofit, CallAdapter<?> wrapped) {
      this.retrofit = retrofit;
      this.wrapped = wrapped;
    }

    @Override
    public Type responseType() {
      return wrapped.responseType();
    }

    @SuppressWarnings("unchecked")
    @Override
    public <R> Observable<?> adapt(Call<R> call) {
      return ((Observable) wrapped.adapt(call))
          .onErrorResumeNext(new Func1<Throwable, Observable>() {
            @Override
            public Observable call(Throwable throwable) {
              return Observable.error(asRetrofitException(throwable));
            }
          });
    }

    private RetrofitException asRetrofitException(Throwable throwable) {
      // 不是200的情況,例如400,401,404等
      if (throwable instanceof HttpException) {
        HttpException httpException = (HttpException) throwable;
        Response response = httpException.response();
        return RetrofitException
            .httpError(httpException.code(), response.raw().request().url().toString(), response,
                retrofit);
      }
      // 網絡出問題和超時處理
      if (throwable instanceof IOException || throwable instanceof TimeoutException) {
        return RetrofitException.networkError((Exception) throwable);
      }
      // 數據解析出現問題(方便調試和快速定位BUG)
      if (throwable instanceof JsonSyntaxException || throwable instanceof JsonParseException
          || throwable instanceof JSONException || throwable instanceof ParseException) {
        return RetrofitException.parseError((Exception) throwable);
      }
      return RetrofitException.unexpectedError(new Throwable());
    }
  }
}

這裏當數據解析異常時,JsonSyntaxException 雖然繼承ParseException但是似乎並沒有卵用,如果不加JsonSyntaxException 異常依舊走不到解析異常,這個要格外注意。
(未完待續。。。)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章