使用Retrofit2和XML时出现Android错误

时间:2016-06-06 08:29:12

标签: android retrofit2

我正在尝试使用Retrofit和Simple XML制作一个简单的Get Request,但是我收到以下错误。我无法理解我做错了什么,因为颜色是一个元素,它不会从任何类别中遗漏。

错误:

I/System.out: FAILED : org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.Element(data=false, name=color, required=true, type=class java.lang.String) on field 'color' public java.lang.String me.limetag.zwz.models.Category.color for class me.limetag.zwz.models.Category at line 4

我的模特:

@Root
public class Categories implements Serializable
{    
@ElementList
private List<Category> category;

public List<Category> getCategory ()
{
    return category;
}

public void setCategory (List<Category> category)
{
    this.category = category;
}

}


@Root
public class Category implements Serializable
{
 @Element(name="id",type=Integer.class)
 public Integer id;

 @Element(name="color",type=String.class)
 public String color;

 @Element(name="name",type=String.class)
 public String name;
}

电话:

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(APIURL)
            .addConverterFactory(SimpleXmlConverterFactory.create())
            .client(new OkHttpClient())
            .build();

    ZService service = retrofit.create(ZService.class);
    service.getCategories().enqueue(new Callback<Categories>() {
        @Override
        public void onResponse(Call<Categories> call, Response<Categories> response) {
            if(response.isSuccessful()) {
                Categories categoryList = response.body();
                List<Category> categories = categoryList.getCategory();
                for (Category category: categories
                     ) {
                        String name = category.getName();
                        Log.e("DONE",name);
                }
            }
        }

XML:

<categories>
<category>
    <id>8</id>
    <name>BEVERAGES</name>
    <color>#00B392</color>
</category>
<category>
    <id>8</id>
    <name>BEVERAGES</name>
    <color>#00B392</color>
</category>
</categories>

修改

我尝试使用简单的XML来反转过程并打印出JAVA类,这就是我所得到的。我不明白从哪里得到额外的类别节点。

<categories>
<category class="java.util.ArrayList">
 <category>
  <color>#000000</color>
  <id>1</id>
  <name>NAME</name>
 </category>
 <category>
  <color>#000000</color>
  <id>1</id>
  <name>NAME</name>
 </category>
 <category>
  <color>#000000</color>
  <id>1</id>
  <name>NAME</name>
 </category>
</category>

0 个答案:

没有答案