Wednesday, April 15, 2015

Android : SAXparser android XML feed trouble



I am very new to website and android app development so please bare with me.


I am having a very similar problem to the following issue. Unfortunately I have not found my anwser after reading through this question.


SAXParser Android, ArrayList repeating elements


I am trying to gather an RSS feed from my website for my android app. I am using the following java script to do so.



public class XMLHelper extends DefaultHandler {

private String URL_MAIN = "http://**.**.***.***/blank/blank/feed/?format=xml";
String TAG = "XMLHelper";

Boolean currTag = false;
String currTagVal = "";
public ItemValue post = null;
public ArrayList<ItemValue> posts = new ArrayList<ItemValue>();


public void get() {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser mSaxParser = factory.newSAXParser();
XMLReader mXmlReader = mSaxParser.getXMLReader();
mXmlReader.setContentHandler(this);
InputStream mInputStream = new URL(URL_MAIN).openStream();
mXmlReader.parse(new InputSource(mInputStream));
} catch(Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}

@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if(currTag) {
currTagVal = currTagVal + new String(ch, start, length);
currTag = false;
}
}


@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
currTag = false;

if(localName.equalsIgnoreCase("title"))
post.setTitle(currTagVal);

else if (localName.equalsIgnoreCase("item")) {
posts.add(post);
}
}

@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
Log.i(TAG, "TAG: " + localName);

currTag = true;
currTagVal = "";

if(localName.equals("item"))
post = new ItemValue();
}


This is pulling from the following xml.



<channel>
<title>Welcome to my website</title>
<link>http://**.**.***.***/blank/blank/feed</link>
<atom:link href="http://**.**.***.***/blank/blank/feed/?format=xml" rel="self" type="application/rss+xml"/>
<description>Activity feed</description>
<lastBuildDate>Wed, 15 Apr 2015 16:21:17 +0000</lastBuildDate>
<generator>http://buddypress.org/?v=2.2.1</generator>
<language>en-US</language>
<ttl>30</ttl>
<sy:updatePeriod>hourly</sy:updatePeriod>
<sy:updateFrequency>2</sy:updateFrequency>
<item>
<guid isPermaLink="false">822b2a07599526230c7bae1b1d0a00bf</guid>
<title>
gordy77 posted an update in the group ****, **: Now the magic begins
</title>
<link>http://**.**.***.***/draft-2/p/9/</link>
<pubDate>Mon, 13 Apr 2015 16:47:29 +0000</pubDate>
<content:encoded>...</content:encoded>
<slash:comments>0</slash:comments>
</item>


Now when I run the app I just get a blank screen and the following error.


E/XMLHelper﹕ Exception: null


By changing the following code I can get the app to run but it only pulls one comment "item" from the RSS feed and repeats it 3 times. It is always the first "item" posted on that feed x3.



if(localName.equals("item"))
post = new ItemValue();

if(localName.equals("channel"))
post = new ItemValue();

No comments:

Post a Comment