无法为类示例创建呼叫适配器。简单

浏览:43日期:2024-02-22
如何解决无法为类示例创建呼叫适配器。简单?

简短的答案:返回Call<Simple>您的服务界面。

看起来Retrofit 2.0正在尝试寻找一种为服务接口创建代理对象的方法。它希望您编写以下代码:

public interface SimpleService { @GET('/simple/{id}') Call<Simple> getSimple(@Path('id') String id);}

但是,当您不想返回时,它仍然希望表现出色并且保持灵活性Call。为了支持这一点,它具有a的概念,该概念CallAdapter应该知道如何将a调整Call<Simple>为aSimple。

RxJavaCallAdapterFactory仅当您尝试返回时,使用才有用rx.Observable<Simple>。

最简单的解决方案是返回CallRetrofit期望的结果。CallAdapter.Factory如果确实需要,也可以编写一个。

解决方法

我正在使用SimpleXml改造2.0.0-beta1。我想从REST服务中检索简单(XML)资源。使用SimpleXML编组/解组Simple对象可以正常工作。

使用此代码(转换为2.0.0之前的代码)时:

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作品包装返回类型。但是我希望服务将业务对象作为类型返回(并在同步模式下工作)。

更新

添加了额外的依赖关系并.addCallAdapterFactory(RxJavaCallAdapterFactory.create())根据不同的答案提出建议后,我仍然收到此错误:

Caused by: java.lang.IllegalArgumentException: Could not locate call adapter for class simple.Simple. Tried: * retrofit.RxJavaCallAdapterFactory * retrofit.DefaultCallAdapter$1

相关文章: