Working with RSS feed
Working with RSS feed
To develop a Android Application that makes use of RSS Feed.
Instructor Note:
Student must read the topic of XML objects and make sure to familiar with the RSS file making and how to read them.
Introduction
The National Weather Service XML format is…curiously structured, relying heavily on sequential position in lists versus the more object-oriented style you find in formats like RSS or Atom. That being said, we can take a few liberties and simplify the parsing somewhat, taking advantage of the fact that the elements we want (start-valid-time for the forecast time, value for the temperature, and icon-link for the icon URL) are all unique within the document. The HTML comes in as an InputStream and is fed into the DOM parser. From there, we scan for the start-valid-time elements and populate a set of Forecast models using those start times. Then, we find the temperature value elements and icon-link URLs and fill those in to the Forecast objects.
RSS actually stands for Really Simple Syndication – an XML-based format for sharing and distributing Web content
A format for delivering for regularly changing web content, like many online publisher, news etc
Activities:
Activity 1:
Procedure:
Creating a New project:
- Open Android Studio and then click on File -> New -> New project.
- Then type the Application name as “no.6″ and click Next.
- Then select the Minimum SDK as shown below and click Next.
- Then select the Empty Activity and click
- Finally click Finish.
- It will take some time to build and load the
- After completion it will look as given
Designing layout for the Android Application:
- Click on app -> res -> layout -> xml
- Now click on Text as shown
- Then delete the code which is there and type the code as given
Code for 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>
- Now click on Design and your application will look as given
- So now the designing part is
Adding permissions in Manifest for the Android Application:
- Click on app -> manifests -> xml
- Now include the INTERNET permissions in the xml file as shown below
<?xml version=”1.0″ encoding=”utf-8″?>
Code for AndroidManifest.xml:
<manifest package=”com.example.exno6″ >
<uses-permission android:name=”android.permission.INTERNET”/>
<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:supportsRtl=”true”
android:theme=”@style/AppTheme” >
<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>
- So now the Permissions are added in the Manifest.
Java Coding for the Android Application:
- Click on app -> java -> example.exno6 -> MainActivity.
- Then delete the code which is there and type the code as given
Code for MainActivity.java:
package com.example.exno6;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
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 ListActivity
{
List headlines;
List links;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
new MyAsyncTask().execute();
}
class MyAsyncTask extends AsyncTask<Object,Void,ArrayAdapter>
{
@Override
protected ArrayAdapter doInBackground(Object[] params)
{
headlines = new ArrayList();
links = new ArrayList(); try
{
URL url = newXmlPullParserFactory 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;
etc..
// Returns the type of current event: START_TAG, END_TAG,
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”))
{
the headline
if (insideItem) headlines.add(xpp.nextText()); //extract
}
else if (xpp.getName().equalsIgnoreCase(“link”))
{
link of article
if (insideItem)
links.add(xpp.nextText()); //extract the
}
}
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)
{
e.printStackTrace();
}
return null;
}
protected void onPostExecute(ArrayAdapter adapter)
{
adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, headlines);
setListAdapter(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();
catch (IOException e)
{
return null;
}
}
}
- So now the Coding part is also
- Now run the application to see the
Output:
Result:
Thus Android Application that makes use of RSS Feed is developed and executed successfully.
Home Activities:
Activity 1:
Read RSS news from the BBC news page and print them on the text view/listview and also save it in the database.