2017-02-08 4 views
3

Я пытаюсь получить данные из URL-адреса xml и получить некоторые проблемы. Пример XML:Как получить xml данные из retrofit 2 и rxjava в android

<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: невозможно создать конвертер для java.util.List для метода APIService.getPerson

Заранее спасибо.

+0

Вы используете RxJava2? –

ответ

1

SimpleXmlConverterFactory не поддерживает список, вам нужно определить результат интерфейса API как массив лицо:

public interface APIService { 

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

см SimpleXmlConverterFactory класс documentation:

Этот конвертер применяется только для типов классов , Параметрированные типы (например, {@code Список}) * не обрабатываются.

Смежные вопросы