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

huangapple go评论96阅读模式
英文:

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

问题

I'm having trouble doing a retrofit request that returns a simple Call object.

I have my retrofit setup done this way:

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

And I have the following retrofit interface which has mixed rxjava and simple calls defined:

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

The issue that I face is when I execute:
myInterface.getCall1().execute(), I get the following error:

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

Should be noted that if I convert Call getCall1() to return Completable getCall1(), everything works as expected.

Anyone has any idea of what I'm doing wrong?

英文:

I'm having trouble doing a retrofit request that returns a simple Call object.

I have my retrofit setup done this way:

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

And I have the following retrofit interface which has mixed rxjava and simple calls defined

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

The issue that I face is when I execute:
myInterface.getCall1().execute() I get the following error

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

Should be noted that if I convert Call getCall1() to return Completable getCall1() everything works as expected.

Anyone has any idea of what I'm doing wrong ?

答案1

得分: 4

我假设RxErrorHandlingCallAdapterFactory此帖子中提到的类。

CallAdapterFactory 的实现应该对其不知道如何适配的类型返回 null。封装的 RxJava2CallAdapterFactory 正确地执行了这一操作,对于 Call 返回类型(通常由内置的 DefaultCallAdapterFactory 处理)返回 null。然而,RxErrorHandlingCallAdapterFactory 会忽略这一点,会返回一个委托给 null 的适配器,当您尝试获取一个 Call 时会导致崩溃。

为了正确指示它无法处理的调用,您的 CallAdapterFactory 应该在包装的工厂返回 null 时返回 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);
}
英文:

I'm assuming RxErrorHandlingCallAdapterFactory is the class mentioned in this post.

Implementations of CallAdapterFactory should return null for a type it doesn't know how to adapt. The wrapped RxJava2CallAdapterFactory does this correctly, and returns null for Call return types (which are normally handled by the built-in DefaultCallAdapterFactory). However, RxErrorHandlingCallAdapterFactory will happily ignore this and return an adapter that delegates to null, causing a crash when you actually try to get a Call.

To correctly indicate calls it can't handle, your CallAdapterFactory should return null whenever the wrapped factory returns null:

  public CallAdapter&lt;?, ?&gt; get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
      CallAdapter wrapped = original.get(returnType, annotations, retrofit);
      if (wrapped = null) {
          return null;
      }

      return new RxCallAdapterWrapper(retrofit, wrapped);
  }

huangapple
  • 本文由 发表于 2020年1月7日 00:42:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615863.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定