IllegalArgumentException:当使用SimpleXmlConverter和改进2.0

时间:2016-05-06 11:09:48

标签: android retrofit

用法

private Retrofit mRetrofit = new Retrofit.Builder()
                .baseUrl("http://www.w3schools.com")
                .addConverterFactory(SimpleXmlConverterFactory.create())
                .build();

服务类

public interface Food{
 @GET("/xml/simple.xml")
    Call<List<FoodId>> getFoodIdType();
}

模型类

@Root(name = "Food")
    public class Food{
        @Element(name = "id")
        private int id;
        @Element(name = "description")
        private String description;
        public Food(){

        }
}

Gradle配置

compile 'com.squareup.retrofit2:retrofit:2.0.0'
compile ('com.squareup.retrofit2:converter-simplexml:2.0.0') {
    exclude module: 'stax'
    exclude module: 'stax-api'
    exclude module: 'xpp3'
}

旁注: 当使用不同的URL时,我能够使它与GsonConverterfactory协同工作。

1 个答案:

答案 0 :(得分:2)

@Root(name = "Food")更改为@Root(name = "food")可能有所帮助。你的整个模型类应该是这样的:

@Root(name = "breakfast_menu") 

public class BreakFastMenu {

    @ElementList(inline = true)
    List<Food> foodList;
}


@Root(name = "food")
class Food {

    @Element(name = "name")
    String name;

    @Element(name = "price")
    String price;

    @Element(name = "description")
    String description;

    @Element(name = "calories")
    String calories;
}