使用XML Converter进行改造返回null

时间:2017-11-01 10:12:00

标签: android retrofit2

我第一次使用RetrofitXML Converter,我的目标是获取项目标记内的描述字段。问题是,每次执行它时,它都会返回description null。

我的XML是:

<rss version="2.0">
<channel>
 <title>Tests</title>
 <description>First Description</description>
 <link>http://teste.html</link>
 <item>
  <title>Test number 5 - 1/11/2017</title>
  <description>
      Description that I want to get
  </description>
  <link> http://test.html </link>
 </item>
</channel>
</rss>

班级频道:

@Root(name="rss", strict=false)
public class Channel {

    @Element(name = "title", required = false)
    private String title;

    @Element(name = "description", required = false)
    private String description;

    @Element(name = "link", required = false)
    private String link;

    private Item item;

    public String getTitle() {return title;}

    public void setTitle(String title) {this.title = title;}

    public String getDescription() {return description;}

    public void setDescription(String description) {this.description = description;}

    public String getitem(){
        Item item = new Item();
        return item.getDescription();
    }

    public void setLink(String title) {this.link = link;}

    public String getLink(String title) { return link;}   
}

班级项目:

@Root(name="item", strict=false)
public class Item {

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

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

    public String getTitle() {return this.title;}

    public void setTitle(String title) {this.title = title;}

    public String getDescription() {return this.description;}

    public void setDescription(String description) {
        this.description = description;
    }
}

我试图打印描述但我得到空的方法。

@Override
    public void onResponse(Call<Channel> call, Response<Channel> response) {
        if (response.isSuccessful()) {
            Channel rss = response.body();
            Log.d("Controller","Description----->: " + rss.getitem());
        } else {
            Log.d("Controller","Error----->:"+response.errorBody());
        }
    }

我有什么不对的吗?

1 个答案:

答案 0 :(得分:1)

在这个问题上花了一些时间后,我找到了解决方案。

问题是我应该在每个class Channel标记declaration的{​​{1}}添加Element

例如:

@Path("channel")