简体   繁体   中英

ValueRequiredException during RSS feed parsing in Retrofit

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM