[英]How to get xml data from retrofit 2 and rxjava in android
I am trying to get data from xml url and getting some problem. 我试图从xml url获取数据并遇到一些问题。 Sample xml: 示例xml:
<company xmlns="http://www.test.com/test">
<person>
<pId>a11</pId>
<name>Mike</name>
<age>25</age>
<weight>82.7</weight>
<profile>www.test.com/mike.jpb</profile>
<person>
<person>
<pId>a11</pId>
<name>Mike</name>
<age>25</age>
<weight>82.7</weight>
<profile>www.test.com/mike.jpb</profile>
<person>
<person>
<pId>a11</pId>
<name>Mike</name>
<age>25</age>
<weight>82.7</weight>
<profile>www.test.com/mike.jpb</profile>
<person>
</company>
what I have done in my java code: 我在java代码中做了什么:
Observable<List<Person>> call = APIClient.getRetrofitAPIClient().getPerson(APIClient.API_PARAMETER);
subscription = call
.subscribeOn(Schedulers.io()) // optional if you do not wish to override the default behavior
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<List<Person>>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
if (e instanceof HttpException) {
HttpException response = (HttpException)e;
int code = response.code();
Log.e("TOTAL", "code "+code);
}
}
@Override
public void onNext(List<Person> p) {
showResults(p);
}
});
Now 现在
public interface APIService {
@GET("/{paramter}")
rx.Observable<List<Person>> getPersons(@Path("paramter") String paramter);
}
and 和
// get retrofit api services
public static APIService getRetrofitAPIClient(){
if(apiService == null){
Retrofit retrofit =new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.client(okHttpClient)
.addCallAdapterFactory(rxJavaCallAdapterFactory)
.addConverterFactory(SimpleXmlConverterFactory.create())
.build();
apiService = retrofit.create(APIService.class);
}
return apiService;
}
Model classes 模型类
@Root(name = "person")
public class Person {
@Element(name = "pId")
private String pId;
@Element(name = "name")
private String name;
@Element(name = "age")
private int age;
@Element(name="weight")
private double weight;
@Element(name="profile")
private String profile;
}
and 和
@Root(name = "company")
public class Company {
@ElementList(entry = "person", inline = true)
private List<Person> companies;
}
I am getting following in logcat: 我在logcat中得到以下内容:
java.lang.RuntimeException: Unable to start activity ComponentInfo{co.test/co.test.activity.MainActivity}: java.lang.IllegalArgumentException: Unable to create converter for java.util.List for method APIService.getPerson java.lang.RuntimeException:无法启动活动ComponentInfo {co.test / co.test.activity.MainActivity}:java.lang.IllegalArgumentException:无法为方法APIService.getPerson创建java.util.List的转换器
Thanks in advance. 提前致谢。
SimpleXmlConverterFactory doesn't support List, you need to define the API interface result as an array of Person: SimpleXmlConverterFactory不支持List,您需要将API接口结果定义为Person数组:
public interface APIService {
@GET("/{paramter}")
rx.Observable<Person[]> getPersons(@Path("paramter") String paramter);
}
see SimpleXmlConverterFactory class documentation : 请参阅SimpleXmlConverterFactory类文档 :
This converter only applies for class types. 此转换器仅适用于类类型。 Parameterized types (eg, {@code List }) are * not handled. *处理参数化类型 (例如,{@code List })。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.