无法阅读 rss XML ,使用 Simple XML 和 Retrofit

Can't read rss XML , with Simple XML and Retrofit

我搜索了很多,但我无法将我的 XML 分配给我的 Classes,并且当我尝试反序列化 XML 时,我从 Retrofit 中获取了错误.

这是我从界面调用的 XML(只有第一项):

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
>
<channel>
    <title>Flow</title>
    <atom:link href="http://flow.com/?feed=single_gallery_feed&#038;paged=0" rel="self" type="application/rss+xml" />
    <link>http://flow.com</link>
    <description></description>
    <lastBuildDate>Mon, 02 Nov 2015 17:07:54 +0000</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <generator>http://wordpress.org/?v=4.3.1</generator>
    <item>
         <title>Pocket Rocket Power</title>
         <link>http://flow.com/portfolio/pocket-rocket-power/</link>
         <comments>http://flow.com/portfolio/pocket-rocket-power/#comments</comments>
         <pubDate>Mon, 02 Nov 2015 17:06:17 +0000</pubDate>
         <dc:creator></dc:creator>
         <category><![CDATA[Accessories]]></category>
         <category><![CDATA[Car Accessories]]></category>
         <category><![CDATA[Cool Gadgets]]></category>
         <category><![CDATA[Crowdfunding Projects]]></category>
         <category><![CDATA[Gift Ideas  - ]]></category>
         <category><![CDATA[iPad Accessories]]></category>
         <category><![CDATA[iPhone 4/4s]]></category>
         <category><![CDATA[iPhone 5/5s]]></category>
         <category><![CDATA[iPhone 6/6s Accessories]]></category>
         <category><![CDATA[iPhone Accessories]]></category>
         <category><![CDATA[Made in USA]]></category>
         <category><![CDATA[Power Banks]]></category>
         <category><![CDATA[Recommended]]></category>
         <category><![CDATA[Samsung Accessories]]></category>
         <category><![CDATA[Travel Must Haves]]></category>
         <guid isPermaLink="false">http://flow.com/?p=81755</guid>
         <content:encoded><![CDATA[<img class="gf-rss-single-image" src="http://cdn.flow.com/wp-content/uploads/2015/11/Pocket-Rocket-Power-01.jpeg" /><img class="gf-rss-single-image" src="http://cdn.flow.com/wp-content/uploads/2015/11/Pocket-Rocket-Power-02.jpeg" /><img class="gf-rss-single-image" src="http://cdn.flow.com/wp-content/uploads/2015/11/Pocket-Rocket-Power-03.jpeg" /><img class="gf-rss-single-image" src="http://cdn.flow.com/wp-content/uploads/2015/11/Pocket-Rocket-Power-04.jpeg" /><img class="gf-rss-single-image" src="http://cdn.flow.com/wp-content/uploads/2015/11/Pocket-Rocket-Power-05.jpeg" /><img class="gf-rss-single-image" src="http://cdn.flow.com/wp-content/uploads/2015/11/Pocket-Rocket-Power-06.jpeg" /><div class="gf-rss-price">Price: $<span class="gf-price">25</span></div><img width="1300" height="1000" src="http://cdn.flow.com/wp-content/uploads/2015/11/Pocket-Rocket-Power-03.jpeg" class="attachment-full wp-post-image" alt="Pocket Rocket Power 03" style="float:none; margin:0 0 15px; display:block;" />
      ]]></content:encoded>
    <slash:comments>0</slash:comments>
</item>

所以我使用异步方法通过回调获取项目,这是 java 接口:

public interface ApiService {

@GET("/?feed=single_gallery_feed")
void getGadgets(@Query("paged")int page, Callback<Channel> gadgetsCallback);
 }

这是正在生成接口的 Rest Client Class:

public class RestClient {

private ApiService apiService;

public RestClient(){
    RestAdapter restAdapter = new RestAdapter.Builder()
            .setLogLevel(RestAdapter.LogLevel.FULL)
            .setClient(new OkClient(new OkHttpClient()))
            .setEndpoint("http://flow.com")
            .setConverter(new SimpleXMLConverter())
            .build();

    apiService = restAdapter.create(ApiService.class);
}

public ApiService getApiService(){
    return apiService;
}
}

此外,我创建了一些模型 Classes 来承载来自 XML 的标签,我省略了 Getters :

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

     @Element(name="channel")
     private Channel channel;

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

@Element(name = "title")
private String title;
@Element(name = "link")
private String link;
@Element(name = "comments")
private String comments;
@Element(name = "pubDate")
private String pubDate;
}

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

@ElementList(name="item",inline = true)
private ArrayList<Item> items;
}

最后我在 MainActivity:

中调用了服务
public class MainActivity extends AppCompatActivity {

private ArrayList<Item> mList;
private Button pagerButton;
private int page = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    pagerButton = (Button)findViewById(R.id.pager);
    pagerButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            RestClient restClient = new RestClient();
            Callback<Channel> callback = new Callback<Channel>() {
                @Override
                public void success(Channel gadgets, Response response) {
                    mList = gadgets.getItems();
                }

                @Override
                public void failure(RetrofitError error) {
                    Log.d("billy",error.getLocalizedMessage());
                }
            };
            restClient.getApiService().getGadgets(page,callback);
                    page++;
        }
    });

    if(mList != null) {
        for(int i = 0; i < mList.size();i++) {
            //mList = mItem.getGadgets();
            Log.d("billy", "Item : " + String.valueOf(i));
            Log.d("billy", "Title: " + mList.get(i).getTitle());
            Log.d("billy", "PubDate: " + mList.get(i).getPubDate());
            Log.d("billy", "ImageURL: " + mList.get(i).getLink());
             Log.d("billy","-------------------------------------------------------------------- ");
        }
    }

}
}

所以,我从改造中得到了这个错误:

org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.ElementList(data=false, empty=true, entry=, inline=true, name=item, required=true, type=void) on field 'items' private java.util.ArrayList Model.Channel.items for class Model.Channel at line 9

我到处搜索,我认为我的实现是正确的,但我找不到错误,我已经多次更改我的模型 类 并且我使用了来自网络的 POJO 生成器,但是再次我无法修复此错误。请帮忙!谢谢!

最终我更改了我的回调并且成功了。例如,我必须在回调中使用模型 Class Rss :

public interface ApiService {
     @GET("/?feed=single_gallery_feed")
     void getGadgets(@Query("paged")int page, Callback<Rss> gadgetsCallback);
}

以及 MainActivity 中的回调:

RestClient restClient = new RestClient();
        Callback callback = new Callback() {
            @Override
            public void success(Object o, Response response) {
                Categories_ = (Categories)o;
            }

            @Override
            public void failure(RetrofitError error) {
                Toast toast = Toast.makeText(getApplication(), "Something went wrong!!", Toast.LENGTH_SHORT);
                toast.show();
            }
        };
        restClient.getApiService().getCategories(callback);