我正在使用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"));
Run Code Online (Sandbox Code Playgroud)
服务:
public interface SimpleService {
@GET("/simple/{id}")
Simple getSimple(@Path("id") String id);
}
Run Code Online (Sandbox Code Playgroud)
我得到这个例外:
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)
Run Code Online (Sandbox Code Playgroud)
我错过了什么?我知道通过Call作品包装返回类型.但是我希望服务将业务对象作为类型返回(并在同步模式下工作).
UPDATE
添加额外的依赖项后,并.addCallAdapterFactory(RxJavaCallAdapterFactory.create())根据不同的答案建议,我仍然会收到此错误:
Caused by: java.lang.IllegalArgumentException: Could not locate call adapter for class simple.Simple. Tried:
* retrofit.RxJavaCallAdapterFactory
* retrofit.DefaultCallAdapter$1
Run Code Online (Sandbox Code Playgroud) 例如,打电话
api.getUserName(userId, new Callback<String>() {...});
Run Code Online (Sandbox Code Playgroud)
原因:
retrofit.RetrofitError: retrofit.converter.ConversionException:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException:
Expected a string but was BEGIN_OBJECT at line 1 column 2
Run Code Online (Sandbox Code Playgroud)
我想我必须禁用gson解析到POJO但无法弄清楚如何做到这一点.
我正在使用Retrofit2和RxJava2CallAdapterFactory.
我使用的API总是将状态代码返回为200,对于成功和响应JSON字符串,JSON结构完全不同.由于状态代码始终为200,因此始终调用onResponse()方法.因此,我无法在错误条件下从JSON中提取错误消息.
解决方案1:
我ScalarsConverterFactory用来获取响应String并手动使用Gson来解析响应.
如何在不使用GSON或android中的任何其他库的情况下使用retrofit获取响应
这个解决方案的问题:我打算使用RxJava2CallAdapterFactory,因为改进方法应该返回DataModel Class.
我需要找到解决此问题的最佳解决方案,我可以继续从Retrofit方法返回数据模型类,并以某种方式从响应中识别错误条件(确定响应JSON与数据模型不匹配)然后解析错误JSON成数据模型.
改造客户
public static Retrofit getClient(String url) {
if (apiClient == null) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient httpClient = new OkHttpClient.Builder().addInterceptor(interceptor).build();
apiClient = new Retrofit.Builder()
.baseUrl(url)
/*addCallAdapterFactory for RX Recyclerviews*/
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
/* add ScalarsConverterFactory to get json string as response */
// .addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
// .addConverterFactory(GsonConverterFactory.create(gson))
.client(httpClient)
.build();
}
return apiClient;
}
Run Code Online (Sandbox Code Playgroud)
方法
public static void getLoginAPIResponse(String username, String password, String sourceId, String uuid, final …Run Code Online (Sandbox Code Playgroud)