如何为 Android 构建 RSS 阅读器?

发布于 2024-11-09 02:30:32 字数 3443 浏览 2 评论 0原文

我是 Android 新手,我正在尝试为 Android 构建一个 RSS 阅读器。我已经构建了所有类和 XML 文件,但它没有提供所需的输出。它只是显示消息 没有可用的 RSS feed

请有人建议我应该做什么。

这是我从教程中获取并尝试操作的代码-

public final String RSSFEEDOFCHOICE = "http://blog.01synergy.com/feed/";

public final String tag = "RSSReader";
private RSSFeed feed = null;

/** Called when the activity is first created. */

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    // go get our feed!
    feed = getFeed(RSSFEEDOFCHOICE);

    // display UI
    UpdateDisplay();

}


private RSSFeed getFeed(String urlToRssFeed)
{
    try
    {
        // setup the url
       URL url = new URL(urlToRssFeed);

       // create the factory
       SAXParserFactory factory = SAXParserFactory.newInstance();
       // create a parser
       SAXParser parser = factory.newSAXParser();

       // create the reader (scanner)
       XMLReader xmlreader = parser.getXMLReader();
       // instantiate our handler
       RSSHandler theRssHandler = new RSSHandler();
       // assign our handler
       xmlreader.setContentHandler(theRssHandler);
       // get our data via the url class
       InputSource is = new InputSource(url.openStream());
       // perform the synchronous parse           
       xmlreader.parse(is);
       // get the results - should be a fully populated RSSFeed instance, or null on error
       return theRssHandler.getFeed();
    }
    catch (Exception ee)
    {
        // if we have a problem, simply return null
        return null;
    }
}
public boolean onCreateOptionsMenu(Menu menu) 
{
    super.onCreateOptionsMenu(menu);

    menu.add(0,0,0, "Choose RSS Feed");
    menu.add(0,1,0, "Refresh");
    Log.i(tag,"onCreateOptionsMenu");
    return true;
}

public boolean onOptionsItemSelected(Menu item){
    switch (((View) item).getId()) {
    case 0:

        Log.i(tag,"Set RSS Feed");
        return true;
    case 1:
        Log.i(tag,"Refreshing RSS Feed");
        return true;
    }
    return false;
}


private void UpdateDisplay()
{
    TextView feedtitle = (TextView) findViewById(R.id.feedtitle);
    TextView feedpubdate = (TextView) findViewById(R.id.feedpubdate);
    ListView itemlist = (ListView) findViewById(R.id.itemlist);


    if (feed == null)
    {
        feedtitle.setText("No RSS Feed Available");
        return;
    }

    feedtitle.setText(feed.getTitle());
    feedpubdate.setText(feed.getPubDate());

    ArrayAdapter<RSSItem> adapter = new ArrayAdapter<RSSItem>(this,android.R.layout.simple_list_item_1,feed.getAllItems());

    itemlist.setAdapter(adapter);

    itemlist.setOnItemClickListener(this);

    itemlist.setSelection(0);

}


 public void onItemClick(AdapterView parent, View v, int position, long id)
 {
     Log.i(tag,"item clicked! [" + feed.getItem(position).getTitle() + "]");

     Intent itemintent = new Intent(this,ShowDescription.class);

     Bundle b = new Bundle();
     b.putString("title", feed.getItem(position).getTitle());
     b.putString("description", feed.getItem(position).getDescription());
     b.putString("link", feed.getItem(position).getLink());
     b.putString("pubdate", feed.getItem(position).getPubDate());

     itemintent.putExtra("android.intent.extra.INTENT", b);

     startSubActivity(itemintent,0);
 }


private void startSubActivity(Intent itemintent, int i) {
    // TODO Auto-generated method stub

}

}

I am new to android and i am trying to build a RSS reader for Android. I have built all the classes and XML files but its not giving the required output. Its just showing the message
No RSS feed available.

Please can some one suggest what should i do.

Here is the code which i took from the tutorial and tried to manipulate-

public final String RSSFEEDOFCHOICE = "http://blog.01synergy.com/feed/";

public final String tag = "RSSReader";
private RSSFeed feed = null;

/** Called when the activity is first created. */

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    // go get our feed!
    feed = getFeed(RSSFEEDOFCHOICE);

    // display UI
    UpdateDisplay();

}


private RSSFeed getFeed(String urlToRssFeed)
{
    try
    {
        // setup the url
       URL url = new URL(urlToRssFeed);

       // create the factory
       SAXParserFactory factory = SAXParserFactory.newInstance();
       // create a parser
       SAXParser parser = factory.newSAXParser();

       // create the reader (scanner)
       XMLReader xmlreader = parser.getXMLReader();
       // instantiate our handler
       RSSHandler theRssHandler = new RSSHandler();
       // assign our handler
       xmlreader.setContentHandler(theRssHandler);
       // get our data via the url class
       InputSource is = new InputSource(url.openStream());
       // perform the synchronous parse           
       xmlreader.parse(is);
       // get the results - should be a fully populated RSSFeed instance, or null on error
       return theRssHandler.getFeed();
    }
    catch (Exception ee)
    {
        // if we have a problem, simply return null
        return null;
    }
}
public boolean onCreateOptionsMenu(Menu menu) 
{
    super.onCreateOptionsMenu(menu);

    menu.add(0,0,0, "Choose RSS Feed");
    menu.add(0,1,0, "Refresh");
    Log.i(tag,"onCreateOptionsMenu");
    return true;
}

public boolean onOptionsItemSelected(Menu item){
    switch (((View) item).getId()) {
    case 0:

        Log.i(tag,"Set RSS Feed");
        return true;
    case 1:
        Log.i(tag,"Refreshing RSS Feed");
        return true;
    }
    return false;
}


private void UpdateDisplay()
{
    TextView feedtitle = (TextView) findViewById(R.id.feedtitle);
    TextView feedpubdate = (TextView) findViewById(R.id.feedpubdate);
    ListView itemlist = (ListView) findViewById(R.id.itemlist);


    if (feed == null)
    {
        feedtitle.setText("No RSS Feed Available");
        return;
    }

    feedtitle.setText(feed.getTitle());
    feedpubdate.setText(feed.getPubDate());

    ArrayAdapter<RSSItem> adapter = new ArrayAdapter<RSSItem>(this,android.R.layout.simple_list_item_1,feed.getAllItems());

    itemlist.setAdapter(adapter);

    itemlist.setOnItemClickListener(this);

    itemlist.setSelection(0);

}


 public void onItemClick(AdapterView parent, View v, int position, long id)
 {
     Log.i(tag,"item clicked! [" + feed.getItem(position).getTitle() + "]");

     Intent itemintent = new Intent(this,ShowDescription.class);

     Bundle b = new Bundle();
     b.putString("title", feed.getItem(position).getTitle());
     b.putString("description", feed.getItem(position).getDescription());
     b.putString("link", feed.getItem(position).getLink());
     b.putString("pubdate", feed.getItem(position).getPubDate());

     itemintent.putExtra("android.intent.extra.INTENT", b);

     startSubActivity(itemintent,0);
 }


private void startSubActivity(Intent itemintent, int i) {
    // TODO Auto-generated method stub

}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

身边 2024-11-16 02:30:32

这是我使用 RSS 阅读器的第一个方法,它不是那么动态并且有样板代码,但对我自己来说效果很好。

用法:

RssParser parser = new RssParser(feedUrl);
Log.i("LOG", "Description: " + parser.getItem(3).description); //4th item's description

类别:

package com.uncocoder.course.app.feed_reader;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;


public class RssParser extends DefaultHandler {

    private StringBuilder   content;
    private boolean         inChannel;
    private boolean         inImage;
    private boolean         inItem;

    private ArrayList<Item> items   = new ArrayList<Item>();
    private Channel         channel = new Channel();

    private Item            lastItem;


    public RssParser(String url) {
        try {
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();
            URL sourceUrl = new URL(url);
            xr.setContentHandler(this);
            xr.parse(new InputSource(sourceUrl.openStream()));
        }
        catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
        catch (SAXException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }


    public class Item {

        public String title;
        public String description;
        public String link;
        public String category;
        public String pubDate;
        public String guid;
    }


    public class Channel {

        public String title;
        public String description;
        public String link;
        public String lastBuildDate;
        public String generator;
        public String imageUrl;
        public String imageTitle;
        public String imageLink;
        public String imageWidth;
        public String imageHeight;
        public String imageDescription;
        public String language;
        public String copyright;
        public String pubDate;
        public String category;
        public String ttl;
    }


    @Override
    public void startDocument() throws SAXException {
        Log.i("LOG", "StartDocument");
    }


    @Override
    public void endDocument() throws SAXException {
        Log.i("LOG", "EndDocument");
    }


    @Override
    public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
        if (localName.equalsIgnoreCase("image")) {
            inImage = true;
        }

        if (localName.equalsIgnoreCase("channel")) {
            inChannel = true;
        }

        if (localName.equalsIgnoreCase("item")) {
            lastItem = new Item();
            items.add(lastItem);
            inItem = true;
        }

        content = new StringBuilder();
    }


    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (localName.equalsIgnoreCase("image")) {
            inImage = false;
        }

        if (localName.equalsIgnoreCase("channel")) {
            inChannel = false;
        }

        if (localName.equalsIgnoreCase("item")) {
            inItem = false;
        }

        if (localName.equalsIgnoreCase("title")) {
            if (content == null) {
                return;
            }

            if (inItem) {
                lastItem.title = content.toString();
            } else if (inImage) {
                channel.imageTitle = content.toString();
            } else if (inChannel) {
                channel.title = content.toString();
            }

            content = null;
        }

        if (localName.equalsIgnoreCase("description")) {
            if (content == null) {
                return;
            }

            if (inItem) {
                lastItem.description = content.toString();
            } else if (inImage) {
                channel.imageDescription = content.toString();
            } else if (inChannel) {
                channel.description = content.toString();
            }

            content = null;
        }

        if (localName.equalsIgnoreCase("link")) {
            if (content == null) {
                return;
            }

            if (inItem) {
                lastItem.link = content.toString();
            } else if (inImage) {
                channel.imageLink = content.toString();
            } else if (inChannel) {
                channel.link = content.toString();
            }

            content = null;
        }

        if (localName.equalsIgnoreCase("category")) {
            if (content == null) {
                return;
            }

            if (inItem) {
                lastItem.category = content.toString();
            } else if (inChannel) {
                channel.category = content.toString();
            }

            content = null;
        }

        if (localName.equalsIgnoreCase("pubDate")) {
            if (content == null) {
                return;
            }

            if (inItem) {
                lastItem.pubDate = content.toString();
            } else if (inChannel) {
                channel.pubDate = content.toString();
            }

            content = null;
        }

        if (localName.equalsIgnoreCase("guid")) {
            if (content == null) {
                return;
            }

            lastItem.guid = content.toString();
            content = null;
        }

        if (localName.equalsIgnoreCase("url")) {
            if (content == null) {
                return;
            }

            channel.imageUrl = content.toString();
            content = null;
        }

        if (localName.equalsIgnoreCase("width")) {
            if (content == null) {
                return;
            }

            channel.imageWidth = content.toString();
            content = null;
        }

        if (localName.equalsIgnoreCase("height")) {
            if (content == null) {
                return;
            }

            channel.imageHeight = content.toString();
            content = null;
        }

        if (localName.equalsIgnoreCase("language")) {
            if (content == null) {
                return;
            }

            channel.language = content.toString();
            content = null;
        }

        if (localName.equalsIgnoreCase("copyright")) {
            if (content == null) {
                return;
            }

            channel.copyright = content.toString();
            content = null;
        }

        if (localName.equalsIgnoreCase("ttl")) {
            if (content == null) {
                return;
            }

            channel.ttl = content.toString();
            content = null;
        }
    }


    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (content == null) {
            return;
        }

        content.append(ch, start, length);
    }


    public Item getItem(int index) {
        return items.get(index);
    }
}

This is my first approach to RSS Reader, It's no so dynamic and has boilerplate code but worked well for myself.

Usage:

RssParser parser = new RssParser(feedUrl);
Log.i("LOG", "Description: " + parser.getItem(3).description); //4th item's description

Class:

package com.uncocoder.course.app.feed_reader;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;


public class RssParser extends DefaultHandler {

    private StringBuilder   content;
    private boolean         inChannel;
    private boolean         inImage;
    private boolean         inItem;

    private ArrayList<Item> items   = new ArrayList<Item>();
    private Channel         channel = new Channel();

    private Item            lastItem;


    public RssParser(String url) {
        try {
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();
            URL sourceUrl = new URL(url);
            xr.setContentHandler(this);
            xr.parse(new InputSource(sourceUrl.openStream()));
        }
        catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
        catch (SAXException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }


    public class Item {

        public String title;
        public String description;
        public String link;
        public String category;
        public String pubDate;
        public String guid;
    }


    public class Channel {

        public String title;
        public String description;
        public String link;
        public String lastBuildDate;
        public String generator;
        public String imageUrl;
        public String imageTitle;
        public String imageLink;
        public String imageWidth;
        public String imageHeight;
        public String imageDescription;
        public String language;
        public String copyright;
        public String pubDate;
        public String category;
        public String ttl;
    }


    @Override
    public void startDocument() throws SAXException {
        Log.i("LOG", "StartDocument");
    }


    @Override
    public void endDocument() throws SAXException {
        Log.i("LOG", "EndDocument");
    }


    @Override
    public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
        if (localName.equalsIgnoreCase("image")) {
            inImage = true;
        }

        if (localName.equalsIgnoreCase("channel")) {
            inChannel = true;
        }

        if (localName.equalsIgnoreCase("item")) {
            lastItem = new Item();
            items.add(lastItem);
            inItem = true;
        }

        content = new StringBuilder();
    }


    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (localName.equalsIgnoreCase("image")) {
            inImage = false;
        }

        if (localName.equalsIgnoreCase("channel")) {
            inChannel = false;
        }

        if (localName.equalsIgnoreCase("item")) {
            inItem = false;
        }

        if (localName.equalsIgnoreCase("title")) {
            if (content == null) {
                return;
            }

            if (inItem) {
                lastItem.title = content.toString();
            } else if (inImage) {
                channel.imageTitle = content.toString();
            } else if (inChannel) {
                channel.title = content.toString();
            }

            content = null;
        }

        if (localName.equalsIgnoreCase("description")) {
            if (content == null) {
                return;
            }

            if (inItem) {
                lastItem.description = content.toString();
            } else if (inImage) {
                channel.imageDescription = content.toString();
            } else if (inChannel) {
                channel.description = content.toString();
            }

            content = null;
        }

        if (localName.equalsIgnoreCase("link")) {
            if (content == null) {
                return;
            }

            if (inItem) {
                lastItem.link = content.toString();
            } else if (inImage) {
                channel.imageLink = content.toString();
            } else if (inChannel) {
                channel.link = content.toString();
            }

            content = null;
        }

        if (localName.equalsIgnoreCase("category")) {
            if (content == null) {
                return;
            }

            if (inItem) {
                lastItem.category = content.toString();
            } else if (inChannel) {
                channel.category = content.toString();
            }

            content = null;
        }

        if (localName.equalsIgnoreCase("pubDate")) {
            if (content == null) {
                return;
            }

            if (inItem) {
                lastItem.pubDate = content.toString();
            } else if (inChannel) {
                channel.pubDate = content.toString();
            }

            content = null;
        }

        if (localName.equalsIgnoreCase("guid")) {
            if (content == null) {
                return;
            }

            lastItem.guid = content.toString();
            content = null;
        }

        if (localName.equalsIgnoreCase("url")) {
            if (content == null) {
                return;
            }

            channel.imageUrl = content.toString();
            content = null;
        }

        if (localName.equalsIgnoreCase("width")) {
            if (content == null) {
                return;
            }

            channel.imageWidth = content.toString();
            content = null;
        }

        if (localName.equalsIgnoreCase("height")) {
            if (content == null) {
                return;
            }

            channel.imageHeight = content.toString();
            content = null;
        }

        if (localName.equalsIgnoreCase("language")) {
            if (content == null) {
                return;
            }

            channel.language = content.toString();
            content = null;
        }

        if (localName.equalsIgnoreCase("copyright")) {
            if (content == null) {
                return;
            }

            channel.copyright = content.toString();
            content = null;
        }

        if (localName.equalsIgnoreCase("ttl")) {
            if (content == null) {
                return;
            }

            channel.ttl = content.toString();
            content = null;
        }
    }


    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (content == null) {
            return;
        }

        content.append(ch, start, length);
    }


    public Item getItem(int index) {
        return items.get(index);
    }
}
七分※倦醒 2024-11-16 02:30:32

检查以下链接,它是适用于 Android 的开源 RSS 阅读器,您可以下载代码以供参考

http:// code.google.com/p/android-rss/

Check following link, It's open source RSS reader for Android, You can download code for reference

http://code.google.com/p/android-rss/

呆° 2024-11-16 02:30:32

这里有一个关于如何构建 Android RSS 渲染器的教程(包括完整的源代码):

第 1 部分(完整应用程序)

第 2 部分(应用程序更新为从描述中解析图像标签)

第 3 部分(Android 3.0 的应用程序更新)

本教程使用 SAX 解析器,并包含一个完整的 Android 项目,该项目访问 RSS 源,然后在列表视图中显示该源。

似乎仍然有很多人对此感兴趣 - 因此,如果您正在寻找带有片段/异步任务等的 Android 3.0+,以及完整的应用程序代码,我再次更新了帖子,并且 可以在这里找到它们!

There is a tutorial (including complete source code) on how to build an Android RSS reder here:

part 1 (complete application)

part 2 (application updated to parse image tags from desciption)

part 3 (application update with Android 3.0)

The tutorial uses SAX parser and includes a complete Android project that accesses an RSS feed and then displays the feed in a list view.

There still seems to be a lot of people interested in this - so if you are looking for an Android 3.0+ with fragments/async tasks etc, as well as complete application code, I have updated the posts again, and they can be found here!

落墨 2024-11-16 02:30:32

您可以在 google-play 上下载并查看我的项目。
这个项目是关于一些土耳其体育频道的内容。一个应用程序中有很多频道。

您可以在 github 上查看项目源代码。

You can download and check my project on google-play.
This project is about some turkish sport channels feeds. Lots of channels are in one application.

You can check source code of project on github.

爱本泡沫多脆弱 2024-11-16 02:30:32

使用 XmlPullParser 轻松解析此类 Rss Feed

< img src="https://i.sstatic.net/R3Aas.png" alt="轻松使用解析此类 Rss Feed XmlPullParser">

     public class RSSParser {

public static ArrayList<Pojo> getParserData(String Data){
    try {
        RSSXMLTag currentTag = null;
        ArrayList<Pojo> postDataList = new ArrayList<>();
        XmlPullParserFactory factory = XmlPullParserFactory
                .newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();
        xpp.setInput(new StringReader(Data));

        int eventType = xpp.getEventType();
        Pojo pdData = null;
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "EEEE, DD MMM yyyy ");
        while (eventType != XmlPullParser.END_DOCUMENT) {
            int i = 0;
            if (eventType == XmlPullParser.START_DOCUMENT) {

            } else if (eventType == XmlPullParser.START_TAG) {
                if (xpp.getName().equals("item")) {

                    pdData = new Pojo();

                    currentTag = RSSXMLTag.IGNORETAG;
                } else if (xpp.getName().equals("title")) {
                    currentTag = RSSXMLTag.TITLE;
                } else if (xpp.getName().equals("link")) {
                    currentTag = RSSXMLTag.LINK;
                } else if (xpp.getName().equals("pubDate")) {
                    currentTag = RSSXMLTag.DATE;
                } else if (xpp.getName().equals("creator")) {

                    currentTag = RSSXMLTag.CREATOR;
                } else if (xpp.getName().equals("category")) {
                    currentTag = RSSXMLTag.CATEGORY;
                } else if (xpp.getName().equals("description")) {
                    currentTag = RSSXMLTag.DESCRIPTION;
                }
            } else if (eventType == XmlPullParser.END_TAG) {
                if (xpp.getName().equals("item")) {
                    // format the data here, otherwise format data in
                    // Adapter
                    Date postDate = dateFormat.parse(pdData.postDate);
                    pdData.postDate = dateFormat.format(postDate);
                    postDataList.add(pdData);
                } else {
                    currentTag = RSSXMLTag.IGNORETAG;
                }
            } else if (eventType == XmlPullParser.TEXT) {
                String content = xpp.getText();
                content = content.trim();
                Log.d("debug", content);
                if (pdData != null) {
                    switch (currentTag) {
                        case TITLE:
                            if (content.length() != 0) {
                                if (pdData.postTitle != null) {
                                    pdData.postTitle += content;
                                } else {
                                    pdData.postTitle = content;
                                }
                            }
                            break;
                        case LINK:
                            if (content.length() != 0) {
                                if (pdData.postLink != null) {
                                    pdData.postLink += content;
                                } else {
                                    pdData.postLink = content;
                                }
                            }
                            break;
                        case DATE:
                            if (content.length() != 0) {
                                if (pdData.postDate != null) {
                                    pdData.postDate += content;
                                } else {
                                    pdData.postDate = content;
                                }
                            }
                            break;
                        case CATEGORY:
                            if (content.length() != 0) {
                                if (pdData.postCategory != null) {
                                    i = i + 1;
                                    if (i == 1) {
                                        pdData.postCategory = content;
                                    }
                                } else {
                                    i = i + 1;
                                    if (i == 1) {
                                        pdData.postCategory = content;
                                    }
                                }
                            }
                            break;
                        case DESCRIPTION:
                            if (content.length() != 0) {
                                if (pdData.postDescription != null) {
                                    String s = content;

                                    String string = s.substring(s.indexOf("src=\"") + 5, s.indexOf("\" class=\""));
                                    pdData.postDescription += string;
                                } else {
                                    String s = content;

                                    String string = s.substring(s.indexOf("src=\"") + 5, s.indexOf("\" class=\""));
                                    pdData.postDescription = string;
                                }
                            }
                            break;
                        case CREATOR:
                            if (content.length() != 0) {
                                if (pdData.postCreator != null) {
                                    pdData.postCreator += content;
                                } else {
                                    pdData.postCreator = content;
                                }
                            }
                            break;
                        default:
                            break;
                    }
                }
            }

            eventType = xpp.next();
        }
        return postDataList;
    }catch (Exception e){
        e.printStackTrace();
    }

    return null;
}

public enum RSSXMLTag {
    IGNORETAG, TITLE, LINK, DATE,CREATOR,CATEGORY, DESCRIPTION;
}


public class Pojo {


public String getPostTitle() {
    return postTitle;
}

public void setPostTitle(String postTitle) {
    this.postTitle = postTitle;
}

public String getPostLink() {
    return postLink;
}

public void setPostLink(String postLink) {
    this.postLink = postLink;
}

public String getPostDate() {
    return postDate;
}

public void setPostDate(String postDate) {
    this.postDate = postDate;
}

public String getPostCategory() {
    return postCategory;
}

public void setPostCategory(String postCategory) {
    this.postCategory = postCategory;
}

public String getPostDescription() {
    return postDescription;
}

public void setPostDescription(String postDescription) {
    this.postDescription = postDescription;
}

public String getPostCreator() {
    return postCreator;
}

public void setPostCreator(String postCreator) {
    this.postCreator = postCreator;
}

public String postTitle;

public String postLink;

public String postDate;

public String postCategory;

public String postDescription;
public String postCreator;
}


}

Parse this type of Rss Feeds easily using XmlPullParser

Parse this type of Rss Feeds easily using <code>XmlPullParser</code>

     public class RSSParser {

public static ArrayList<Pojo> getParserData(String Data){
    try {
        RSSXMLTag currentTag = null;
        ArrayList<Pojo> postDataList = new ArrayList<>();
        XmlPullParserFactory factory = XmlPullParserFactory
                .newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();
        xpp.setInput(new StringReader(Data));

        int eventType = xpp.getEventType();
        Pojo pdData = null;
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "EEEE, DD MMM yyyy ");
        while (eventType != XmlPullParser.END_DOCUMENT) {
            int i = 0;
            if (eventType == XmlPullParser.START_DOCUMENT) {

            } else if (eventType == XmlPullParser.START_TAG) {
                if (xpp.getName().equals("item")) {

                    pdData = new Pojo();

                    currentTag = RSSXMLTag.IGNORETAG;
                } else if (xpp.getName().equals("title")) {
                    currentTag = RSSXMLTag.TITLE;
                } else if (xpp.getName().equals("link")) {
                    currentTag = RSSXMLTag.LINK;
                } else if (xpp.getName().equals("pubDate")) {
                    currentTag = RSSXMLTag.DATE;
                } else if (xpp.getName().equals("creator")) {

                    currentTag = RSSXMLTag.CREATOR;
                } else if (xpp.getName().equals("category")) {
                    currentTag = RSSXMLTag.CATEGORY;
                } else if (xpp.getName().equals("description")) {
                    currentTag = RSSXMLTag.DESCRIPTION;
                }
            } else if (eventType == XmlPullParser.END_TAG) {
                if (xpp.getName().equals("item")) {
                    // format the data here, otherwise format data in
                    // Adapter
                    Date postDate = dateFormat.parse(pdData.postDate);
                    pdData.postDate = dateFormat.format(postDate);
                    postDataList.add(pdData);
                } else {
                    currentTag = RSSXMLTag.IGNORETAG;
                }
            } else if (eventType == XmlPullParser.TEXT) {
                String content = xpp.getText();
                content = content.trim();
                Log.d("debug", content);
                if (pdData != null) {
                    switch (currentTag) {
                        case TITLE:
                            if (content.length() != 0) {
                                if (pdData.postTitle != null) {
                                    pdData.postTitle += content;
                                } else {
                                    pdData.postTitle = content;
                                }
                            }
                            break;
                        case LINK:
                            if (content.length() != 0) {
                                if (pdData.postLink != null) {
                                    pdData.postLink += content;
                                } else {
                                    pdData.postLink = content;
                                }
                            }
                            break;
                        case DATE:
                            if (content.length() != 0) {
                                if (pdData.postDate != null) {
                                    pdData.postDate += content;
                                } else {
                                    pdData.postDate = content;
                                }
                            }
                            break;
                        case CATEGORY:
                            if (content.length() != 0) {
                                if (pdData.postCategory != null) {
                                    i = i + 1;
                                    if (i == 1) {
                                        pdData.postCategory = content;
                                    }
                                } else {
                                    i = i + 1;
                                    if (i == 1) {
                                        pdData.postCategory = content;
                                    }
                                }
                            }
                            break;
                        case DESCRIPTION:
                            if (content.length() != 0) {
                                if (pdData.postDescription != null) {
                                    String s = content;

                                    String string = s.substring(s.indexOf("src=\"") + 5, s.indexOf("\" class=\""));
                                    pdData.postDescription += string;
                                } else {
                                    String s = content;

                                    String string = s.substring(s.indexOf("src=\"") + 5, s.indexOf("\" class=\""));
                                    pdData.postDescription = string;
                                }
                            }
                            break;
                        case CREATOR:
                            if (content.length() != 0) {
                                if (pdData.postCreator != null) {
                                    pdData.postCreator += content;
                                } else {
                                    pdData.postCreator = content;
                                }
                            }
                            break;
                        default:
                            break;
                    }
                }
            }

            eventType = xpp.next();
        }
        return postDataList;
    }catch (Exception e){
        e.printStackTrace();
    }

    return null;
}

public enum RSSXMLTag {
    IGNORETAG, TITLE, LINK, DATE,CREATOR,CATEGORY, DESCRIPTION;
}


public class Pojo {


public String getPostTitle() {
    return postTitle;
}

public void setPostTitle(String postTitle) {
    this.postTitle = postTitle;
}

public String getPostLink() {
    return postLink;
}

public void setPostLink(String postLink) {
    this.postLink = postLink;
}

public String getPostDate() {
    return postDate;
}

public void setPostDate(String postDate) {
    this.postDate = postDate;
}

public String getPostCategory() {
    return postCategory;
}

public void setPostCategory(String postCategory) {
    this.postCategory = postCategory;
}

public String getPostDescription() {
    return postDescription;
}

public void setPostDescription(String postDescription) {
    this.postDescription = postDescription;
}

public String getPostCreator() {
    return postCreator;
}

public void setPostCreator(String postCreator) {
    this.postCreator = postCreator;
}

public String postTitle;

public String postLink;

public String postDate;

public String postCategory;

public String postDescription;
public String postCreator;
}


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文