英文:
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<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
CallAdapter wrapped = original.get(returnType, annotations, retrofit);
if (wrapped = null) {
return null;
}
return new RxCallAdapterWrapper(retrofit, wrapped);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论