无法为类example.Simple创建调用适配器 [英] Unable to create call adapter for class example.Simple
问题描述
我正在使用SimpleXml改装2.0.0-beta1。我想从REST服务中检索一个简单(XML)资源。
使用SimpleXML编组/解组Simple对象可以正常工作。
I am using retrofit 2.0.0-beta1 with SimpleXml. I want the retrieve a Simple (XML) resource from a REST service. Marshalling/Unmarshalling the Simple object with SimpleXML works fine.
使用此代码时(转换后的2.0.0代码形式):
When using this code (converted form pre 2.0.0 code):
final Retrofit rest = new Retrofit.Builder()
.addConverterFactory(SimpleXmlConverterFactory.create())
.baseUrl(endpoint)
.build();
SimpleService service = rest.create(SimpleService.class);
LOG.info(service.getSimple("572642"));
服务:
public interface SimpleService {
@GET("/simple/{id}")
Simple getSimple(@Path("id") String id);
}
我得到这个例外:
Exception in thread "main" java.lang.IllegalArgumentException: Unable to create call adapter for class example.Simple
for method SimpleService.getSimple
at retrofit.Utils.methodError(Utils.java:201)
at retrofit.MethodHandler.createCallAdapter(MethodHandler.java:51)
at retrofit.MethodHandler.create(MethodHandler.java:30)
at retrofit.Retrofit.loadMethodHandler(Retrofit.java:138)
at retrofit.Retrofit$1.invoke(Retrofit.java:127)
at com.sun.proxy.$Proxy0.getSimple(Unknown Source)
我缺少什么?我知道用 Call
包装返回类型是有效的。但我希望服务将业务对象作为类型返回(并以同步模式工作)。
What am i missing? I know that wrapping the return type by a Call
works. But I want the service to return business objects as type (and working in sync mode).
更新
根据不同答案的建议添加额外的依赖项和 .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
之后,我仍然会收到此错误:
After added the extra dependancies and .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
as suggested by different answers, I still get this error:
Caused by: java.lang.IllegalArgumentException: Could not locate call adapter for class simple.Simple. Tried:
* retrofit.RxJavaCallAdapterFactory
* retrofit.DefaultCallAdapter$1
推荐答案
简答:返回 在您的服务界面中调用< Simple>
。
Short answer: return Call<Simple>
in your service interface.
看起来Retrofit 2.0正试图找到一种方式为您的服务接口创建代理对象。它希望你写这个:
It looks like Retrofit 2.0 is trying to find a way of creating the proxy object for your service interface. It expects you to write this:
public interface SimpleService {
@GET("/simple/{id}")
Call<Simple> getSimple(@Path("id") String id);
}
然而,当你不这样做时,它仍然想玩得好想要返回致电
。为了支持这一点,它具有 <$ c $的概念c> CallAdapter ,应该知道如何将 Call< Simple>
改编成 Simple
。
However, it still wants to play nice and be flexible when you don't want to return a Call
. To support this, it has the concept of a CallAdapter
, which is supposed to know how to adapt a Call<Simple>
into a Simple
.
使用 RxJavaCallAdapterFactory
仅在您尝试返回<时才有用code> rx.Observable< Simple> 。
最简单的解决方案是返回 Call
正如Retrofit预期的那样。你也可以写一个 CallAdapter .Factory
如果你真的需要它。
The simplest solution is to return a Call
as Retrofit expects. You could also write a CallAdapter.Factory
if you really need it.
这篇关于无法为类example.Simple创建调用适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!