Android Pull解析RSS Feed烦恼

问题描述:

我正在研究Android的一个非常简单的RSS阅读器作为学习体验。我决定使用XmlPullParser来解析提要,因为它非常简单并且具有可接受的效率水平(符合我的需要)。在尝试解析我的测试提要(rss.slashdot.org/slashdot/slashdot)时出现错误,我似乎无法解决,尽管在网上寻找答案。 (从蚀)的错误是:Android Pull解析RSS Feed烦恼

START_TAG <image>@2:1252 in [email protected] 
START_TAG (empty) <{http://www.w3.org/2005/Atom}atom10:link rel='self' type='application/rss+xml' href='http://rss.slashdot.org/Slashdot/slashdot'>@2:1517 in [email protected] 
DEBUG/JRSS(313): java.net.MalformedURLException: Protocol not found: 

在问题的文件是:

<image> 
    ... 
</image> 
<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://rss.slashdot.org/Slashdot/slashdot" /> 
<feedburner:info uri="slashdot/slashdot" /> 
<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /> 
    ... 

所以错误出现在FeedBurner的标签出现。

终于,我的代码是:

public class XmlHelper 
{ 
    private XmlPullParserFactory factory; 
    private XmlPullParser xpp; 
    private final int START_TAG = XmlPullParser.START_TAG; 

    // Debugging Tag 
    private final String TAG = "JRSS"; 

    // for channels and items 
    private final String TITLE = "title"; 
    private final String LINK = "link"; 
    private final String DESCRIPTION = "description"; 
    private final String PUBDATE = "pubDate"; 

    // element keys for channel 
    private final String LANGUAGE = "language"; 
    private final String IMAGE = "image"; 
    private final String ITEM = "item"; 

    // for items 
    private final String AUTHOR = "author"; 

    // for images 
    private final String URL = "url"; 
    private final String WIDTH = "width"; 
    private final String HEIGHT = "height"; 

    public XmlHelper(Context context) 
    { 
     try 
    { 
     factory = XmlPullParserFactory.newInstance(); 
    } 
    catch (XmlPullParserException e) 
    { 
     Log.d(TAG, e.toString()); 
    } 
     factory.setNamespaceAware(true); 
    } 

    public Channel addFeed(URL url) throws XmlPullParserException, IOException 
    {  
     Channel c = new Channel(); 
     c.items = new ArrayList<Item>(); 

     xpp = factory.newPullParser(); 
     xpp.setInput(url.openStream(), null); 

     // move past rss element 
     xpp.nextTag(); 
     // move past channel element 
     xpp.nextTag(); 

     while(xpp.nextTag() == START_TAG) 
     { 
      Log.d(TAG, xpp.getPositionDescription()); 

      if(xpp.getName().equals(TITLE)) 
       c.title = xpp.nextText(); 

      else if(xpp.getName().equals(LINK)) 
       c.url = new URL(xpp.nextText()); 

      else if(xpp.getName().equals(DESCRIPTION)) 
       c.description = xpp.nextText(); 

      else if(xpp.getName().equals(LANGUAGE)) 
       c.language = xpp.nextText(); 

      else if(xpp.getName().equals(ITEM)) 
      { 
       Item i = parseItem(xpp); 
       c.items.add(i); 
      } 

      else if(xpp.getName().equals(IMAGE)) 
      { 
       parseImage(xpp); 
      } 

      else 
       xpp.nextText(); 
     } 

     return c; 
    } 

    public Item parseItem(XmlPullParser xpp) throws MalformedURLException, XmlPullParserException, IOException 
    { 
    Item i = new Item(); 

    while(xpp.nextTag() == START_TAG) 
    { 
      // do nothing for now 
     xpp.nextText(); 
    } 

     return i; 
    } 

    private void parseImage(XmlPullParser xpp) throws XmlPullParserException, IOException 
    { 
     // do nothing for now 
     while(xpp.nextTag() == START_TAG) 
     { 
      xpp.nextText(); 
     } 
    } 

我真的不知道,如果有一种方法只是忽略这一点(因为在这一点上我不关心的是FeedBurner标签)或有是解析器的一些功能,我可以设置这个功能,或者如果我以错误的方式解决这个问题。任何帮助/建议/指导将不胜感激。

PullParsing比SAX更高效。但在我看来,它仍然留下了很多需要做的事情,让您的RSS提要能够解析任何提要。

您需要满足RSS 1,RSS 2,Atom等所有格式。即使这样,您也必须应对格式不正确的Feed。

我以前遇到过类似的问题,所以决定在服务器上执行我的feed解析并获取解析的内容。这使我可以运行更复杂的库和解析器,我可以修改它们而不必为我的应用程序推送更新。您应该查看服务器端选项,以便您可以保持轻量级和简单的应用程序。

我有以下服务在AppEngine上运行,它允许在您的最后更简单的XML/JSON解析。答案有一个固定和简单的结构。您可以使用此解析

http://evecal.appspot.com/feedParser

您可以发送POST和GET具有下列参数的请求。

feedLink:RSS馈送响应的URL:JSON或XML作为响应格式

实例:

对于POST请求

卷曲--data-进行urlencode“feedLink = HTTP: //feeds.bbci.co.uk/news/world/rss.xml” --data-进行urlencode “响应= JSON” http://evecal.appspot.com/feedParser

对于GET请求

evecal。 appspot.com/feedParser?feedLink=http://feeds.nytimes.com/nyt/rss/HomePage & response = xml

我的android应用程序“NewsSpeak”也使用它。