What is RSS FEED?
* RSS(originally RDF Site Summary, often dubbed Really Simple Syndication) is a family of webfeed formats used to publish frequently updated works—such as blog entries, news headlines, audio, and video—in a standardized format.
* An RSS document (which is called a "feed", "web feed", or "channel") includes full or summarized text, plus metadata such as publishing dates and authorship.
* Many news-related sites, weblogs and other online publishers syndicate their content as an RSS Feed to whoever wants it.
RSS URL : http://feeds.feedburner.com/iamvijayakumar/androidtutorial
i write code for my blog rss feed reader applcation . In this application every day it will update if any new post added in my blog.
i write code for my blog rss feed reader applcation . In this application every day it will update if any new post added in my blog.
Screen Shot:
Normal Mode |
Sort Mode |
Activity code:
public class RssFeedReaderActivity extends Activity {
/** Called when the activity is first created. */
ListView _rssFeedListView;
List jobs ;
List rssStr ;
private RssReaderListAdapter _adapter;
String sorti = "";
String mode = "";
Button sort_Btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rssfeedreaderactivity);
_rssFeedListView = (ListView)findViewById(R.id.rssfeed_listview);
sort_Btn = (Button)findViewById(R.id.sort);
sort_Btn.setText("Change Sorting Mode");
sort_Btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(sorti.equalsIgnoreCase("")){
sorti = "sort";
}
if(sorti.equalsIgnoreCase("sort")){
sorti = "sort";
sort_Btn.setText("Change Reverse Mode");
RssFeedTask rssTask = new RssFeedTask();
rssTask.execute();
}
else if(sorti.equalsIgnoreCase("reverse")){
sorti = "reverse";
sort_Btn.setText("Change Normal Mode");
RssFeedTask rssTask = new RssFeedTask();
rssTask.execute();
}
else if(sorti.equalsIgnoreCase("normal")){
sort_Btn.setText("Change Sorting Mode");
RssFeedTask rssTask = new RssFeedTask();
rssTask.execute();
}
}
});
RssFeedTask rssTask = new RssFeedTask();
rssTask.execute();
}
private class RssFeedTask extends AsyncTask {
// private String Content;
private ProgressDialog Dialog;
String response = "";
@Override
protected void onPreExecute() {
Dialog = new ProgressDialog(RssFeedReaderActivity.this);
Dialog.setMessage("Rss Loading...");
Dialog.show();
}
@Override
protected String doInBackground(String... urls) {
try {
String feed = "http://feeds.feedburner.com/iamvijayakumar/androidtutorial";
XmlHandler rh = new XmlHandler();
rssStr = rh.getLatestArticles(feed);
} catch (Exception e) {
}
return response;
}
@Override
protected void onPostExecute(String result) {
if(sorti.equalsIgnoreCase("sort")){
sorti = "reverse";
Collections.sort(rssStr, new SortingOrder());
}else if(sorti.equalsIgnoreCase("reverse")){
sorti = "normal";
Comparator comp = Collections.reverseOrder();
Collections.sort(rssStr, new ReverseOrder());
}else{
sorti = "";
}
if(rssStr != null){
_adapter = new RssReaderListAdapter(RssFeedReaderActivity.this,rssStr);
_rssFeedListView.setAdapter(_adapter);
}
Dialog.dismiss();
}
}
}
XML handler class
public class XmlHandler extends DefaultHandler {
private RssFeedStructure feedStr = new RssFeedStructure();
private List rssList = new ArrayList();
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:content"))
{
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 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;
}
}
Layout Code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#5D5C5C"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:background="@layout/shape"
android:layout_height="40dip"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:layout_marginTop="2dip"
android:layout_marginLeft="30dip"
android:gravity="center"
android:textSize="8pt"
android:textStyle="bold"
android:layout_gravity="center"
android:text="iamvijayakumar.blogspot.com"
/>
</LinearLayout>
<ListView
android:layout_width="fill_parent"
android:layout_height="390dip"
android:id="@+id/rssfeed_listview"
android:background="#5D5C5C"
android:cacheColorHint="#00000000"
android:divider="#000000"
android:dividerHeight="1dip"
android:paddingLeft="5pt"
android:paddingRight="5pt"
android:transcriptMode="alwaysScroll"
></ListView>
<LinearLayout
android:layout_width="fill_parent"
android:background="#000000"
android:gravity="bottom"
android:layout_gravity="bottom"
android:layout_height="wrap_content"
>
<Button
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Sort"
android:id="@+id/sort"
/>
</LinearLayout>
</LinearLayout>
This code copied from here
ReplyDeletehttp://iamvijayakumar.blogspot.in/2012/06/android-rss-feed-reader-example.html