Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to create converter for my class in Android Retrofit2 converter-simplexml library

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;
}
like image 335
user6472735 Avatar asked Jun 16 '16 10:06

user6472735


People also ask

What is retrofit library in Android?

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.

What is the latest version of retrofit?

retrofit2:retrofit:2.9. 0 .

What is the difference between retrofit and OkHttp?

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.


1 Answers

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

like image 174
darwin Avatar answered Oct 26 '22 23:10

darwin