Monday, April 15, 2013

[android help] Read only the specific categories from the RSS Feed list


I'm trying to learn how to make RSS Reader for my android app by following this tutorial. The feed is generated from a wordpress blog and I want to figure out a way to read by categories. It's currently reading the entire feed items but I'm trying to sort out specific categories from the list. I'm new to android and java programming and please help kindly, and please let me know if I need to update my question for clarification.


This is how the XML Looks like and I want to pull the FeatureStory1, FeatureStory2, FeatureStory3 and so on..


Thank you for reading.




..




..
..



..




..
..



..




..
..



This is the Activity class to read the feed.



// Connected - Start parsing
new AsyncLoadXMLFeed().execute();

}

}

private void startLisActivity(RSSFeed feed) {

Bundle bundle = new Bundle();
bundle.putSerializable("feed", feed);

// launch List activity
Intent intent = new Intent(SplashActivity.this, GridActivity.class);
intent.putExtras(bundle);
startActivity(intent);

// kill this activity
finish();

}

private class AsyncLoadXMLFeed extends AsyncTask {

@Override
protected Void doInBackground(Void... params) {

// Obtain feed
DOMParser myParser = new DOMParser();
feed = myParser.parseXml(http://mywordpressblog.com/feed/);
if (feed != null && feed.getItemCount() > 0)
WriteFeed(feed);
return null;
}

@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);

startLisActivity(feed);
}

}

// Method to write the feed to the File
private void WriteFeed(RSSFeed data) {

FileOutputStream fOut = null;
ObjectOutputStream osw = null;

try {
fOut = openFileOutput(fileName, MODE_PRIVATE);
osw = new ObjectOutputStream(fOut);
osw.writeObject(data);
osw.flush();
}

catch (Exception e) {
e.printStackTrace();
}

finally {
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

// Method to read the feed from the File
private RSSFeed ReadFeed(String fName) {

FileInputStream fIn = null;
ObjectInputStream isr = null;

RSSFeed _feed = null;
File feedFile = getBaseContext().getFileStreamPath(fileName);
if (!feedFile.exists())
return null;

try {
fIn = openFileInput(fName);
isr = new ObjectInputStream(fIn);

_feed = (RSSFeed) isr.readObject();
}

catch (Exception e) {
e.printStackTrace();
}

finally {
try {
fIn.close();
} catch (IOException e) {
e.printStackTrace();
}
}

return _feed;

}


This is the DOMParser class



try {
// Create required instances
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();

// Parse the xml
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();

// Get all tags.
NodeList nl = doc.getElementsByTagName("item");
int length = nl.getLength();

for (int i = 0; i < length; i++) {
Node currentNode = nl.item(i);
RSSItem _item = new RSSItem();

NodeList nchild = currentNode.getChildNodes();
int clength = nchild.getLength();

// Get the required elements from each Item
for (int j = 1; j < clength; j = j + 2) {

Node thisNode = nchild.item(j);
String theString = null;
String nodeName = thisNode.getNodeName();

theString = nchild.item(j).getFirstChild().getNodeValue();

if (theString != null) {
if ("title".equals(nodeName)) {
// Node name is equals to 'title' so set the Node
// value to the Title in the RSSItem.
_item.setTitle(theString);
}

else if ("content:encoded".equals(nodeName)) {
_item.setDescription(theString);

// Parse the html description to get the image url
String html = theString;
org.jsoup.nodes.Document docHtml = Jsoup
.parse(html);
Elements imgEle = docHtml.select("img");
_item.setImage(imgEle.attr("src"));
}

else if ("pubDate".equals(nodeName)) {

// We replace the plus and zero's in the date with
// empty string
String formatedDate = theString.replace(" +0000",
"");
_item.setDate(formatedDate);
}

}
}


.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...