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

眉间皱痕 提交于 2019-12-13 19:54:43

问题


I have searched a lot, but I can't assign my XML with My Classes and I take an Error from Retrofit when I am trying to Deserialize the XML.

This is the XML that I call from my Interface ( only the first Item):

<?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 $20 - $50]]></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>

So I Use Asynchronous method to take the items with a Callback, This is the java Interface :

public interface ApiService {

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

This is the Rest Client Class where the Interface is being generated:

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

Also , I have created some Model Classes to host the Tags from the XML , I omit 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;
}

Finally I call the service in the 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","-------------------------------------------------------------------- ");
        }
    }

}
}

So, I take this error from Retrofit :

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

I have searched everywhere and I think that my Implementation is correct but I can't find the mistake, I have changed many times my Model classes and I have used POJO generators from the web , but again I can't fix this error. Please help! Thank you!


回答1:


Eventually I changed my Callback and it Worked . I had to use the Model Class Rss in the Callback for example :

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

And the Callback inside 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);


来源:https://stackoverflow.com/questions/33528543/cant-read-rss-xml-with-simple-xml-and-retrofit

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