根据日期和时间显示Rss Feed?

时间:2015-05-15 06:37:12

标签: android date time xml-parsing rss

我正在为一个网站开发RSS阅读器但是当我检索RSS提要时,它不是根据日期和时间,请检查屏幕截图。有人可以通过向我展示如何在我的应用程序中显示最新的Feed来帮助我。

https://www.dropbox.com/s/n9pgbnwazorncnl/20150514233219%20%281%29.jpg?dl=0

这是我的代码:

public class NewsFeedParser {
    private InputStream urlStream;
    private XmlPullParserFactory factory;
    private XmlPullParser parser;
    private List<RSSFeed> rssFeedList;
    private RSSFeed rssFeed;
    private String urlString;
    private String tagName;
    private String title;
    private String link;
    private String description;
    private String category;
    private String pubDate;
    private String guid;
    private String feedburner;
    public static final String ITEM = "item";
    public static final String CHANNEL = "channel";
    public static final String TITLE = "title";
    public static final String LINK = "link";
    public static final String DESCRIPTION = "description";
    public static final String CATEGORY = "category";
    public static final String PUBLISHEDDATE = "pubDate";
    public static final String GUID = "guid";
    public static final String FEEDBURNERORIGLINK = "feedburner:origLink";
    public NewsFeedParser(String urlString) {
        this.urlString = urlString;
    }
    public static InputStream downloadStream(String urlString) throws IOException {
        URL url = new URL(urlString);
       HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        conn.connect();
        InputStream stream = conn.getInputStream();
        return stream;
    }
    public List<RSSFeed> parseXmlData() {
        try {
            int count = 0;
            factory = XmlPullParserFactory.newInstance();
            parser = factory.newPullParser();
            urlStream = downloadStream(urlString);
            parser.setInput(urlStream, null);
            int eventType = parser.getEventType();
            boolean done = false;
            rssFeed = new RSSFeed();
            rssFeedList = new ArrayList<RSSFeed>();
            while (eventType != XmlPullParser.END_DOCUMENT && !done) {
                tagName = parser.getName();
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        break;
                    case XmlPullParser.START_TAG:
                        if (tagName.equals(ITEM)) {
                            rssFeed = new RSSFeed();
                        }
                        if (tagName.equals(TITLE)) {
                            title = parser.nextText().toString();
                        }
                        if (tagName.equals(LINK)) {
                            link = parser.nextText().toString();
                        }
                        if (tagName.equals(DESCRIPTION)) {
                            description = parser.nextText().toString();
                        }
                        if (tagName.equals(CATEGORY)) {
                            category = parser.nextText().toString();
                        }
                        if (tagName.equals(PUBLISHEDDATE)) {
                            pubDate = parser.nextText().toString();
                        }
                        if (tagName.equals(GUID)) {
                            guid = parser.nextText().toString();
                        }
                        if (tagName.equals(FEEDBURNERORIGLINK)) {
                            feedburner = parser.nextText().toString();
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if (tagName.equals(CHANNEL)) {
                            done = true;
                        } else if (tagName.equals(ITEM)) {
                            rssFeed = new RSSFeed(title, link, description, category, pubDate,
                                    guid,
                                    feedburner);
                            Log.i("RSSFeedItem", title + " " + link + " " + description + " " + category + " " + pubDate + guid + " " + feedburner);
                            rssFeedList.add(rssFeed);

                        }
                        break;
                }
                eventType = parser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.i("RSSCount", " : "+Integer.toString(rssFeedList.size()).toString());
        return rssFeedList;
    }
}

1 个答案:

答案 0 :(得分:0)

在RSSFeed类中尝试此类

    //Shorting data w.r.t weight in descending order
    public static class DateComparator implements Comparator<RSSFeed> {
        public int compare(RSSFeed r1, RSSFeed r2) {
            Date date1 = r1.getDate();
            Date date2 = r2.getDate();

            return date1.compareTo(date2);
        }
    }

在传递给适配器

之前,在listview类中使用它
Collections.sort(yourDataList, Collections.reverseOrder(new RSSFeed.DateComparator ()));