故障调用失败,尝试在空对象引用上调用接口方法 'java.lang.reflect.Type retrofit2.CallAdapter.responseType()'

Trouble call fails with Attempt to invoke interface method 'java.lang.reflect.Type retrofit2.CallAdapter.responseType()' on a null object reference

我在执行 return 一个简单的 Call 对象的改造请求时遇到问题。

我的改造设置是这样完成的:

 new Retrofit.Builder()
                .addCallAdapterFactory(RxErrorHandlingCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(gson));

我有以下改造接口,它混合了 rxjava 和定义的简单调用

MyInterface {
   Single getSingle1();
   Single getSingle2();
   Call getCall1();
}

我遇到的问题是执行时: myInterface.getCall1().execute() 我收到以下错误

Attempt to invoke interface method 'java.lang.reflect.Type retrofit2.CallAdapter.responseType()' on a null object reference

应该注意的是,如果我将 Call getCall1() 转换为 return Completable getCall1() 一切都按预期工作。

有人知道我做错了什么吗?

我假设 RxErrorHandlingCallAdapterFactory 中提到的 class。

CallAdapterFactory 的实现应该 return null 对于它不知道如何适应的类型。包装的 RxJava2CallAdapterFactory 正确地做到了这一点,并且 returns null 对于 Call return 类型(通常由内置的 DefaultCallAdapterFactory 处理)。但是,RxErrorHandlingCallAdapterFactory 会很高兴地忽略这个和 return 委托给 null 的适配器,当您实际尝试获取 Call.

时会导致崩溃

为了正确指示它无法处理的调用,只要包装工厂 return 为 null:

,您的 CallAdapterFactory 就应该 return null
  public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
      CallAdapter wrapped = original.get(returnType, annotations, retrofit);
      if (wrapped = null) {
          return null;
      }

      return new RxCallAdapterWrapper(retrofit, wrapped);
  }