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.
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"));
Service:
public interface SimpleService {
@GET("/simple/{id}")
Simple getSimple(@Path("id") String id);
}
I get this exception:
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)
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).
UPDATE
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
If you are not using RxJava it properly makes no sense to add RxJava just for retrofit.
2.5.0
has support forCompletableFuture
built in which you can use without adding any other library or adapter.build.gradle.kts
Api.kt
Usage:
You can implement a Callback, get the Simple from onResponse function.
Communication with the network is done with the separate thread so you should change your Simple with that.
Just to make the Call examples clearer for people who are migrating, not using Rx, or who want synchronous calls - Call essentially replaces (wraps) Response, meaning:
becomes
and not
(which will still require an adapter)
The Call will then allow you to still use
isSuccessful
as it actually returns a Response. So you can do something like:Or access your Type (MyObject) like:
Add the following dependencies for retrofit 2
for GSON
for observables
In your case for XML , you would have to include the following dependencies
Update the service call as below
add dependencies:
create your adapter this way:
addCallAdapterFactory ()
andaddConverterFactory ()
both need to be called.Service:
Modify
Simple
toCall<Simple>
.