porthfind
porthfind

Reputation: 1619

Retrofit with XML Converter returns null

I'm using Retrofit with XML Converter for the first time and my goal is to get the description field that is inside of item's tag. The problem is, every time I execute it, it returns description null.

My XML is:

<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>

Class Channel:

@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;}   
}

Class Item:

@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;
    }
}

The method where I'm trying to print the description but I get null.

@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());
        }
    }

Is there anything that I'm doing wrong?

Upvotes: 1

Views: 640

Answers (1)

porthfind
porthfind

Reputation: 1619

After spending some time with this issue I found the solution.

The problem was that I should have add on class Channel in the declaration of each Element the tag @Path("channel").

For instance:

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

Upvotes: 1

Related Questions