如何从android中的retrofit 2和rxjava获取xml数据

时间:2017-02-08 03:32:20

标签: android rx-java retrofit2 rx-android simple-framework

我正在尝试从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>

我在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);
                    }
                });

现在

public interface APIService {

    @GET("/{paramter}")
    rx.Observable<List<Person>> getPersons(@Path("paramter") String paramter);
}

// 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;
    }

模型类

@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;
}

@Root(name = "company")
public class Company {
    @ElementList(entry = "person", inline = true)
    private List<Person> companies;
}

我在logcat中得到了关注:

  

java.lang.RuntimeException:无法启动活动   ComponentInfo {co.test / co.test.activity.MainActivity}:   java.lang.IllegalArgumentException:无法为其创建转换器   方法APIService.getPerson

的java.util.List

提前致谢。

1 个答案:

答案 0 :(得分:1)

SimpleXmlConverterFactory不支持List,您需要将API接口结果定义为Person数组:

public interface APIService {

     @GET("/{paramter}")
    rx.Observable<Person[]> getPersons(@Path("paramter") String paramter);
}

请参阅SimpleXmlConverterFactory类documentation

  

此转换器仅适用于类类型。 参数化类型   (例如,{@ code 列表})未得到处理。