ValueRequiredException during RSS feed parsing in Retrofit

只愿长相守 提交于 2019-12-25 06:21:47

问题


I'm trying to write a simple RSS reader app for Android. I'm using Retrofit for network communication with SimpleXMLConverter to populate POJOs from XML. This is my REST API:

package ru.unatco.rss.data;

import java.util.List;

import retrofit.Callback;
import retrofit.http.GET;

public interface RssAdapter {
    @GET("/radio-t")
    void getItems(Callback<RadioTItems> callback);
}

This is the code that calls API:

RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint("http://feeds.rucast.net")
                .setConverter(new SimpleXMLConverter())
                .build();

RssAdapter rssAdapter = restAdapter.create(RssAdapter.class);
rssAdapter.getItems(new Callback<RadioTItems>() {
    @Override
    public void success(RadioTItems radioTItems, Response response) {
        System.out.println(radioTItems.toString());
    }
    @Override
    public void failure(RetrofitError error) {
        System.out.println(error);
    }
});

These are the POJOs that I'm trying to get:

package ru.unatco.rss.data;

import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

import java.util.List;

@Root(name = "rss", strict = false)
public class RadioTItems {
    @ElementList(required = false, name = "channel")
    List<RadioTItem> items;
}

package ru.unatco.rss.data;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

@Root(name = "item", strict = false)
public class RadioTItem {
    @Element(name = "title")
    String mTitle;
    @Element(name = "description")
    String mDescription;

    public RadioTItem() {}
}

The source of RSS is here. Now when I'm trying to execute the request I get this exception:

retrofit.RetrofitError: org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.Element(data=false, name=description, required=true, type=void) on field 'mDescription' java.lang.String ru.unatco.rss.data.RadioTItem.mDescription for class ru.unatco.rss.data.RadioTItem at line 2

If I'm setting mDescription and mTitle fields in RadioTItem @Element(required = false) parsing finishes OK but both fields are nulls. Any ideas how to get rid of this problem?


回答1:


OK, after a number of try-fail cycles I've found a working solution. I've completely re-written my POJOs and this is what I've got:

package ru.unatco.rss.data;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

@Root(name = "rss", strict = false)
public class Feed {
    @Element(name = "channel")
    private Channel mChannel;

    public Channel getmChannel() {
        return mChannel;
    }
}

package ru.unatco.rss.data;

import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import ru.unatco.rss.model.Item;

@Root(name = "channel", strict = false)
public class Channel {
    @ElementList(inline = true)
    private List<FeedItem> mFeedItems;

    public List<FeedItem> getFeedItems() {
        return mFeedItems;
    }
}

package ru.unatco.rss.data;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

import ru.unatco.rss.model.Item;

@Root(name = "item", strict = false)
public class FeedItem {
    @Element(name = "title")
    private String mTitle;
    @Element(name = "description")
    private String mDescription;

    public String getTitle() {
        return mTitle;
    }

    public String getDescription() {
        return mDescription;
    }
}

With all this changes a was able to get what I wanted.



来源:https://stackoverflow.com/questions/31202666/valuerequiredexception-during-rss-feed-parsing-in-retrofit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!