The Android development tools does not require that everything is included into one project.
Android library projects allow to store source code and resources which are used by several other Android projects. Library projects cannot be compiled to Android applications directly.
Using library projects help us to structure your application code. Also more and more important Open Source libraries are available for Android. Understanding library projects is therefore important for every Android programmer.
Create a new Android project called com.jitesh.rssfeed.library. Do not need to create an activity.
Create a class with the name XmlHandler.java inside the com.jitesh.rssfeed.library and have a following code
package com.jitesh.rssfeed.library;
import java.io.IOException;
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;
public class XmlHandler extends DefaultHandler {
private RssFeedStructure feedStr = new RssFeedStructure();
private List<RssFeedStructure> rssList = new ArrayList<RssFeedStructure>();
private int articlesAdded = 0;
// Number of articles to download
private static final int ARTICLES_LIMIT = 25;
StringBuffer chars = new StringBuffer();
public void startElement(String uri, String localName, String qName,
Attributes atts) {
chars = new StringBuffer();
if (qName.equalsIgnoreCase("media:thumbnail"))
{
if (!atts.getValue("url").toString().equalsIgnoreCase("null")) {
feedStr.setImgLink(atts.getValue("url").toString());
} else {
feedStr.setImgLink("");
}
}
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (localName.equalsIgnoreCase("title")) {
feedStr.setTitle(chars.toString());
} else if (localName.equalsIgnoreCase("description")) {
feedStr.setDescription(chars.toString());
} else if (localName.equalsIgnoreCase("pubDate")) {
feedStr.setPubDate(chars.toString());
} else if (localName.equalsIgnoreCase("encoded")) {
feedStr.setEncodedContent(chars.toString());
} else if (qName.equalsIgnoreCase("media:content"))
{
} else if (localName.equalsIgnoreCase("link")) {
}
if (localName.equalsIgnoreCase("item")) {
rssList.add(feedStr);
feedStr = new RssFeedStructure();
articlesAdded++;
if (articlesAdded >= ARTICLES_LIMIT) {
throw new SAXException();
}
}
}
public void characters(char ch[], int start, int length) {
chars.append(new String(ch, start, length));
}
public List<RssFeedStructure> getLatestArticles(String feedUrl) {
URL url = null;
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
url = new URL(feedUrl);
xr.setContentHandler(this);
xr.parse(new InputSource(url.openStream()));
} catch (IOException e) {
} catch (SAXException e) {
} catch (ParserConfigurationException e) {
}
return rssList;
}
}
now Create a class with the name RssFeedStructure .java inside the com.jitesh.rssfeed.library and have a following code
package com.jitesh.rssfeed.library;
import java.net.URL;
public class RssFeedStructure {
private long articleId;
private long feedId;
private String title;
private String description;
private String imgLink;
private String pubDate;
private URL url;
private String encodedContent;
public long getArticleId() {
return articleId;
}
public void setArticleId(long articleId) {
this.articleId = articleId;
}
public long getFeedId() {
return feedId;
}
/**
* @param feedId the feedId to set
*/
public void setFeedId(long feedId) {
this.feedId = feedId;
}
/**
* @return the title
*/
public String getTitle() {
return title;
}
/**
* @param title the title to set
*/
public void setTitle(String title) {
this.title = title;
}
/**
* @return the url
*/
public URL getUrl() {
return url;
}
/**
* @param url the url to set
*/
public void setUrl(URL url) {
this.url = url;
}
/**
* @param description the description to set
*/
public void setDescription(String description) {
this.description = description;
if (description.contains("<img ")){
String img = description.substring(description.indexOf("<img "));
String cleanUp = img.substring(0, img.indexOf(">")+1);
img = img.substring(img.indexOf("src=") + 5);
int indexOf = img.indexOf("'");
if (indexOf==-1){
indexOf = img.indexOf("\"");
}
img = img.substring(0, indexOf);
//setImgLink(img);
this.description = this.description.replace(cleanUp, "");
}
}
/**
* @return the description
*/
public String getDescription() {
return description;
}
/**
* @param pubDate the pubDate to set
*/
public void setPubDate(String pubDate) {
this.pubDate = pubDate;
}
/**
* @return the pubDate
*/
public String getPubDate() {
return pubDate;
}
/**
* @param encodedContent the encodedContent to set
*/
public void setEncodedContent(String encodedContent) {
this.encodedContent = encodedContent;
}
/**
* @return the encodedContent
*/
public String getEncodedContent() {
return encodedContent;
}
/**
* @param imgLink the imgLink to set
*/
public void setImgLink(String imgLink) {
this.imgLink = imgLink;
}
/**
* @return the imgLink
*/
public String getImgLink() {
return imgLink;
}
}
find the library code to parse the rss feeds at LibraryForRSSreader
use the following library as follows
String feed = "http://www.ddinews.gov.in/rssCMS/";
XmlHandler rh = new XmlHandler();
List<RssFeedStructure> rssStr=null;
rssStr = rh.getLatestArticles(feed);
Use library project
In your application project defines that you want to use the library project via the project properties.
go to
your's project-->properties-->android-->add--(choose library)-->ok
No comments:
Post a Comment