Rich Site Summary(RSS) Form Feeds in Android

Blog Single

RSS is used to publish frequently updated information: blog entries, news headlines, audio, video. An RSS document (called "feed", "web feed“ or "channel") includes full or summarized text, and metadata, like publishing date and author's name.It often called as Really Simple Syndication.Generally a syndication is the supply of material for reuse and integration with other material, often through a paid service subscription.

Add required permissions to AndroidManifest file
<uses-permission android:name="android.permission.INTERNET"/>
Create an inner class on MainActivity which should extends AsyncTask to handle background tasks
Define the following steps under doInBackground() method
URL url = new URL("http://www.thehindu.com/news/national/tamil-nadu/?service=rss");

//This is an example of “thehindu.com”
Implement Xml Pull Parser Factory class which is used to create implementations of XML Pull Parser defined in XMPULL V1 API
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
The Xml Pull Parser is for parsing the xml data’s of publisher(like thehindu.com,timesofindia.com) XML Pull Parser is an interface that defines parsing functionality provided in XMLPULL V1 AP
XmlPullParser parser = factory.newPullParser();
parser.setInput(getInputStream(url), "UTF_8");
Get the list of information’s available in the xml file
boolean inItem = false;

int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
    if (eventType == XmlPullParser.START_TAG) {

        if (parser.getName().equalsIgnoreCase("item")) {
            inItem = true;
        } else if (parser.getName().equalsIgnoreCase("title")) {
            if (inItem)
                headlines.add(parser.nextText());           //headlines is an array list
        } else if (parser.getName().equalsIgnoreCase("link")) {
            if (inItem)
                links.add(parser.nextText());           //links is an array list
        } else if (parser.getName().equalsIgnoreCase("category")) {
            if (inItem)
                places.add(parser.nextText());          //places is an array list
        } else if (parser.getName().equalsIgnoreCase("description")) {
            if (inItem)
                descriptions.add(parser.nextText());        //descriptions is an array list
        } else if (parser.getName().equalsIgnoreCase("pubDate")) {
            if (inItem)
                dateTime.add(parser.nextText());            //dataTime is an array list
        }
    } else if (eventType == XmlPullParser.END_TAG && parser.getName().equalsIgnoreCase("item")) {
        inItem = false;
    }
    eventType = parser.next();
}


Note:These things should be done inside try and multi-catch which should handle the exceptions of 
MalformedURLException, XmlPullParserException, IOException
Then maintain those arrays on session management through shared preferences. The following example is for storing and retrieving places list on sessions
public void addPlaces(ArrayList places) {
    editor.putInt(KEY_PLACES, places.size());
    int i = 0;
    for (String item : places) {
        editor.putString("place" + i++, item); //"place" is a key
    }
    //Log.e("Places onSessions", String.valueOf(places));
    editor.commit();
}

public ArrayList getPlaces() {
    placesCount = preferences.getInt(KEY_PLACES, 0);

    ArrayList arrayList = new ArrayList(placesCount);

    for (int i = 0; i < placesCount; i++) {
        arrayList.add(preferences.getString("place" + i, null)); //"place" is a key
    }

    //Log.e("Places List", String.valueOf(arrayList));
    return arrayList;
}


Note: As same as this need to do code for headlines, descriptions, links and date_time and 
Fetch those sessions on onCreate() of MainActivity and list them onto listview
Set on click listener of list view for whenever user clicks on specific item that has to be redirect to specific news feed of the specified URL
cListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        Uri uri = Uri.parse(link_s.get(position));
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }
});

Share this post:

Posted by Abdul Gaffar

He is an enthusiastic android engineer at Arivista Digital, his area of expertise is Core Java, Sqlite, Realm, noSQL concepts and json and his area of specialization is very eager to newer technologies.