CNN RSS feed解析在android中不起作用

时间:2011-11-04 10:36:27

标签: android rss

我有以下RSS Parse活动类,     package net.snovabits;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

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.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class AndroidRssReader extends ListActivity {

 private List<String> item = new ArrayList<String>();

 StringBuffer  sb=new StringBuffer();;
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

       try {
   URL rssUrl = new URL("http://www.fchorgen.ch/?feed=rss2");
   SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
   SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
   XMLReader myXMLReader = mySAXParser.getXMLReader();
   RSSHandler myRSSHandler = new RSSHandler();
   myXMLReader.setContentHandler(myRSSHandler);
   InputSource myInputSource = new InputSource(rssUrl.openStream());
   myXMLReader.parse(myInputSource);

  } catch (MalformedURLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (ParserConfigurationException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (SAXException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }      

   }




 private class RSSHandler extends DefaultHandler
   {   

  @Override
  public void startDocument() throws SAXException {
   // TODO Auto-generated method stub
  }

  @Override
  public void endDocument() throws SAXException {
   // TODO Auto-generated method stub
  }

  @Override
  public void startElement(String uri, String localName, String qName,
    Attributes attributes) throws SAXException {
      sb=new StringBuffer();

  }

  @Override
  public void endElement(String uri, String localName, String qName)
    throws SAXException {
   // TODO Auto-generated method stub

   if (localName.equalsIgnoreCase("title"))
   {
       System.out.println("--------title------------"+sb.toString());

   }
  }

  @Override
  public void characters(char[] ch, int start, int length)
    throws SAXException {
   // TODO Auto-generated method stub   
   sb.append(new String(ch, start, length));

  }

   }
}

我使用CNN rss URL来获取rss feed,但它不起作用,它显示错误。但是某些URL似乎工作正常。

我也发布了错误日志,

11-04 10:29:03.226: ERROR/AndroidRuntime(984): Caused by: java.lang.RuntimeException: org.xmlpull.v1.XmlPullParserException: Error parsing document. (position:line -1, column -1) caused by: org.apache.harmony.xml.ExpatParser$ParseException: At line 7, column 11: not well-formed (invalid token)
11-04 10:29:03.226: ERROR/AndroidRuntime(984):     at ca.mobicartel.cgrparser.XmlFeedParser.parse(XmlFeedParser.java:79)
11-04 10:29:03.226: ERROR/AndroidRuntime(984):     at ca.mobicartel.cgrparser.MainActivity$DownloadEpisodes.doInBackground(MainActivity.java:68)
11-04 10:29:03.226: ERROR/AndroidRuntime(984):     at ca.mobicartel.cgrparser.MainActivity$DownloadEpisodes.doInBackground(MainActivity.java:1)
11-04 10:29:03.226: ERROR/AndroidRuntime(984):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
11-04 10:29:03.226: ERROR/AndroidRuntime(984):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-04 10:29:03.226: ERROR/AndroidRuntime(984):     ... 4 more
11-04 10:29:03.226: ERROR/AndroidRuntime(984): Caused by: org.xmlpull.v1.XmlPullParserException: Error parsing document. (position:line -1, column -1) caused by: org.apache.harmony.xml.ExpatParser$ParseException: At line 7, column 11: not well-formed (invalid token)
11-04 10:29:03.226: ERROR/AndroidRuntime(984):     at org.apache.harmony.xml.ExpatPullParser$ByteDocument.flush(ExpatPullParser.java:959)
11-04 10:29:03.226: ERROR/AndroidRuntime(984):     at org.apache.harmony.xml.ExpatPullParser$Document.pump(ExpatPullParser.java:770)
11-04 10:29:03.226: ERROR/AndroidRuntime(984):     at org.apache.harmony.xml.ExpatPullParser$Document.dequeue(ExpatPullParser.java:814)
11-04 10:29:03.226: ERROR/AndroidRuntime(984):     at org.apache.harmony.xml.ExpatPullParser.next(ExpatPullParser.java:303)
11-04 10:29:03.226: ERROR/AndroidRuntime(984):     at ca.mobicartel.cgrparser.XmlFeedParser.parse(XmlFeedParser.java:76)

我不知道原因是什么,但我查看XML资源与其他rss feed xml格式相比有所不同。如果有人知道这会帮助我。

感谢。

1 个答案:

答案 0 :(得分:0)

您需要向INTERNET

添加AndroidManifest.xml权限