简体   繁体   中英

Able to parse RSS feed file in Java project, unable in Android

I have a Java Project in Eclipse where I download the CNN technology RSS feed , then use SAX to parse and get the number of items in the feed.

//download file
URL url = new URL(address);
InputStream is = url.openStream();
FileOutputStream fos = new FileOutputStream("newsfeed.xml");

byte[] buffer = new byte[1024];
int bytesRead = is.read(buffer);
while (bytesRead != -1) {
    fos.write(buffer, 0, bytesRead);
    bytesRead = is.read(buffer);
}

fos.close();
is.close();

//read file
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();

RSSHandler handler = new RSSHandler();
reader.setContentHandler(handler);

FileInputStream fis = new FileInputStream("newsfeed.xml");
InputSource is = new InputSource(fis);
reader.parse(is);

However when I take this code in Android, I get a Parser Exception on the line

reader.parse(is);

The only code I change in Android is

//download file
FileOutputStream out = context.openFileOutput(FILE_PATH, Context.MODE_PRIVATE);

//read file
FileInputStream stream = context.openFileInput(FILE_PATH);

any idea why this happens? or how to fix it?

The exact error I get is a SAXException with the message "At line 1, column 0: not well-formed (invalid token)"

Try this..

Use Below url

http://iamvijayakumar.blogspot.in/2012/06/android-rss-feed-reader-example.html

RSS Parser with CustomAdapter using AsyncTask

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM