2016-05-06 23 views
2

用法拋出:IllegalArgumentException:無法使用SimpleXmlConverter與改型2.0

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

Service類

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

Model類

時的java.util.List 創建轉換器
@Root(name = "Food") 
    public class Food{ 
     @Element(name = "id") 
     private int id; 
     @Element(name = "description") 
     private String description; 
     public Food(){ 

     } 
} 

搖籃配置

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,工作。

+0

更改@root(NAME = 「食品」)以@root(NAME = 「食物」)可能會幫助.... –

回答

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; 
} 
相關問題