Ex.
No: 10 Write a mobile application that makes use of RSS feed
Aim
To develop an android application that makes use of RSS feed.
Procedure
1. Open Android Studio.
2. Create a new project named as exno10.
3. In activity_main.xml add a ListView.
4. In MainActivity.java, create the objects for the components defined in the
activity_main.xml file.
5. Create a resources reference variable and instantiate it with Resources from
getResources() method.
6. Create a String array and initiate it with getStringArray() method of Resources object.
7. Set the Adapter to the ListView with built-in simple_list_item_1 layout and String array
as parameters .
8. In AndroidManifest.xml, get permission to use the INTERNET.
<uses-permission android:name="android.permission.INTERNET"/>
9. Run the exno10 application using an emulator.
Source Code
MainActivity.java
package com.example.exno10;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
66
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
List headlines;
List links;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
new MyAsyncTask().execute();
class MyAsyncTask extends AsyncTask<Object,Void,ArrayAdapter>
67
{
@Override
protected ArrayAdapter doInBackground(Object[] params)
headlines = new ArrayList();
links = new ArrayList();
try
URL url = new URL("https://codingconnect.net/feed");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
// We will get the XML from an input stream
xpp.setInput(getInputStream(url), "UTF_8");
boolean insideItem = false;
// Returns the type of current event: START_TAG, END_TAG, etc..
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
if (eventType == XmlPullParser.START_TAG)
if (xpp.getName().equalsIgnoreCase("item"))
insideItem = true;
else if (xpp.getName().equalsIgnoreCase("title"))
68
{
if (insideItem)
headlines.add(xpp.nextText()); //extract the headline
else if (xpp.getName().equalsIgnoreCase("link"))
if (insideItem)
links.add(xpp.nextText()); //extract the link of article
else if(eventType==XmlPullParser.END_TAG &&
xpp.getName().equalsIgnoreCase("item"))
insideItem=false;
eventType = xpp.next(); //move to next element
catch (MalformedURLException e)
e.printStackTrace();
catch (XmlPullParserException e)
e.printStackTrace();
catch (IOException e)
69
e.printStackTrace();
return null;
protected void onPostExecute(ArrayAdapter adapter)
adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1,
headlines);
setListAdapter(adapter);
private void setListAdapter(ArrayAdapter adapter) {
//@Override
protected void onListItemClick(ListView l, View v, int position, long id)
Uri uri = Uri.parse((links.get(position)).toString());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
public InputStream getInputStream(URL url)
try
return url.openConnection().getInputStream();
70
catch (IOException e)
return null;
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exno10">
<uses-permission android:name="android.permission.INTERNET"/>
71
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Exno10">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
72
Output
Conclusion
Thus, the android application that makes use of RSS feed has been written, executed and output is
also verified.
73