Im using Retrofit2 converter-simplexml library,the code run successful when I using converter-gson,but when I add simplexmlConverter,I got a exception:
java.lang.IllegalArgumentException: Unable to create converter for java.util.List<com.rengwuxian.rxjavasamples.model.ZhuangbiImage>
Caused by: java.lang.IllegalArgumentException: Could not locate ResponseBody converter for java.util.List<com.rengwuxian.rxjavasamples.model.ZhuangbiImage>.
This is where I am trying to execute the retro http request:
private void search(String key) {
subscription = getZhuangbiApi()
.search(key)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(observer);
}
public static ZhuangbiApi getZhuangbiApi() {
if (zhuangbiApi == null) {
Retrofit retrofit = new Retrofit.Builder()
.client(okHttpClient)
.baseUrl(baseUrl)
.addConverterFactory(simpleXmlConverterFactory)
.addCallAdapterFactory(rxJavaCallAdapterFactory)
.build();
zhuangbiApi = retrofit.create(ZhuangbiApi.class);
}
return zhuangbiApi;
}
My interface which turned to be my API
public interface ZhuangbiApi {
@GET("merchant/list")
Observable<List<ZhuangbiImage>> search(@Query("app_code") String appCode);
}
And the ZhuangbiImage class
@Root(name = "item")
public class ZhuangbiImage {
@Element(name = "title")
public String title;
@Element(name = "merchant_id")
public String merchantId;
}
Retrofit is a type-safe REST client for Android, Java and Kotlin developed by Square. The library provides a powerful framework for authenticating and interacting with APIs and sending network requests with OkHttp.
retrofit2:retrofit:2.9. 0 .
OkHttp is a pure HTTP/SPDY client responsible for any low-level network operations, caching, requests and responses manipulation. In contrast, Retrofit is a high-level REST abstraction build on top of OkHttp. Retrofit is strongly coupled with OkHttp and makes intensive use of it.
XML can only be deserialized to a concrete type and not a list of types like JSON. In this case, the body type should represent the <rss>
tag and its children and not List<ZhuangbiImage>
.
More details from official retrofit team Retrofit, XML and SimpleXmlConverterFactory problem
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With