0% found this document useful (0 votes)
43 views10 pages

EX NO: 11 Rss Feed Date: Aim: Algorithm

This document describes an Android application that uses an RSS feed. It provides code for setting up the Android project, retrieving the RSS feed data from a service using a ResultReceiver, parsing the XML data into custom objects, and displaying the results in a list. Key classes include MainActivity, RssFragment, RssService, RssAdapter, RssItem, and PcWorldRssParser.

Uploaded by

Priyadarshini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views10 pages

EX NO: 11 Rss Feed Date: Aim: Algorithm

This document describes an Android application that uses an RSS feed. It provides code for setting up the Android project, retrieving the RSS feed data from a service using a ResultReceiver, parsing the XML data into custom objects, and displaying the results in a list. Key classes include MainActivity, RssFragment, RssService, RssAdapter, RssItem, and PcWorldRssParser.

Uploaded by

Priyadarshini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

EX NO: 11 RSS FEED

DATE:

AIM:
To develop an application that makes use of RSS Feed.
ALGORITHM:
1)Open eclipse or android studio and select new android project
2)Give project name and select next
3) Choose the android version.Choose the lowest android version(Android 2.2) and select next
4) Enter the package name.package name must be two word separated by comma and click finish
5)Go to package explorer in the left hand side.select our project.
6)Go to res folder and select layout.Double click and edit the main.xml file.
7) Now select and edit the main activity file.
8)Select run as option and select run configuration
10) Android output is present in the android emulator as shown in below.
//MainActivity.java
package com.madlab.rssfeed;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends FragmentActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

if (savedInstanceState == null) {
addRssFragment();
}
}

private void addRssFragment() {


FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
RssFragment fragment = new RssFragment();
transaction.add(R.id.fragment_container, fragment);
transaction.commit();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("fragment_added", true);
}
}

//RssFragment.java
package com.madlab.rssfeed;

import java.util.List;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.os.ResultReceiver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

public class RssFragment extends Fragment implements OnItemClickListener {

private ProgressBar progressBar;


private ListView listView;
private View view;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
if (view == null) {
view = inflater.inflate(R.layout.fragment_layout, container, false);
progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
listView = (ListView) view.findViewById(R.id.listView);
listView.setOnItemClickListener(this);
startService();
} else {
// If we are returning from a configuration change:
// "view" is still attached to the previous view hierarchy
// so we need to remove it and re-attach it to the current one
ViewGroup parent = (ViewGroup) view.getParent();
parent.removeView(view);
}
return view;
}

private void startService() {


Intent intent = new Intent(getActivity(), RssService.class);
intent.putExtra(RssService.RECEIVER, resultReceiver);
getActivity().startService(intent);
}

/**
* Once the {@link RssService} finishes its task, the result is sent to this ResultReceiver.
*/
private final ResultReceiver resultReceiver = new ResultReceiver(new Handler()) {
@SuppressWarnings("unchecked")
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
List<RssItem> items = (List<RssItem>) resultData.getSerializable(RssService.ITEMS);
if (items != null) {
RssAdapter adapter = new RssAdapter(getActivity(), items);
listView.setAdapter(adapter);
} else {
Toast.makeText(getActivity(), "An error occured while downloading the rss feed.",
Toast.LENGTH_LONG).show();
}
progressBar.setVisibility(View.GONE);
listView.setVisibility(View.VISIBLE);
};
};

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
RssAdapter adapter = (RssAdapter) parent.getAdapter();
RssItem item = (RssItem) adapter.getItem(position);
Uri uri = Uri.parse(item.getLink());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
}

//RssService.java
package com.madlab.rssfeed;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.net.URL;
import java.util.List;

import org.xmlpull.v1.XmlPullParserException;

import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.provider.SyncStateContract.Constants;
import android.support.v4.os.ResultReceiver;
import android.util.*;

public class RssService extends IntentService {

private static final String RSS_LINK = "http://www.pcworld.com/index.rss";


public static final String ITEMS = "items";
public static final String RECEIVER = "receiver";

public RssService() {
super("RssService");
}

@Override
protected void onHandleIntent(Intent intent) {
// Log.d(Constants.TAG, "Service started");
List<RssItem> rssItems = null;
try {
PcWorldRssParser parser = new PcWorldRssParser();
rssItems = parser.parse(getInputStream(RSS_LINK));
} catch (XmlPullParserException e) {
Log.w(e.getMessage(), e);
} catch (IOException e) {
Log.w(e.getMessage(), e);
}
Bundle bundle = new Bundle();
bundle.putSerializable(ITEMS, (Serializable) rssItems);
ResultReceiver receiver = intent.getParcelableExtra(RECEIVER);
receiver.send(0, bundle);
}
public InputStream getInputStream(String link) {
try {
URL url = new URL(link);
return url.openConnection().getInputStream();
} catch (IOException e) {
// Log.w(Constants.TAG, "Exception while retrieving the input stream", e);
return null;
}
}
}

//RssAdapter.java
package com.madlab.rssfeed;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

public class RssAdapter extends BaseAdapter {

private final List<RssItem> items;


private final Context context;

public RssAdapter(Context context, List<RssItem> items) {


this.items = items;
this.context = context;
}

@Override
public int getCount() {
return items.size();
}

@Override
public Object getItem(int position) {
return items.get(position);
}

@Override
public long getItemId(int id) {
return id;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(context, R.layout.rss_item, null);
holder = new ViewHolder();
holder.itemTitle = (TextView) convertView.findViewById(R.id.itemTitle);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.itemTitle.setText(items.get(position).getTitle());
return convertView;
}

static class ViewHolder {


TextView itemTitle;
}

//RssItem.java
package com.madlab.rssfeed;

public class RssItem {

private final String title;


private final String link;

public RssItem(String title, String link) {


this.title = title;
this.link = link;
}

public String getTitle() {


return title;
}

public String getLink() {


return link;
}
}

//PcWorldRssParser.java
package com.madlab.rssfeed;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Xml;

public class PcWorldRssParser {

// We don't use namespaces


private final String ns = null;

public List<RssItem> parse(InputStream inputStream) throws XmlPullParserException, IOException


{
try {
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(inputStream, null);
parser.nextTag();
return readFeed(parser);
} finally {
inputStream.close();
}
}

private List<RssItem> readFeed(XmlPullParser parser) throws XmlPullParserException,


IOException {
parser.require(XmlPullParser.START_TAG, null, "rss");
String title = null;
String link = null;
List<RssItem> items = new ArrayList<RssItem>();
while (parser.next() != XmlPullParser.END_DOCUMENT) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("title")) {
title = readTitle(parser);
} else if (name.equals("link")) {
link = readLink(parser);
}
if (title != null && link != null) {
RssItem item = new RssItem(title, link);
items.add(item);
title = null;
link = null;
}
}
return items;
}

private String readLink(XmlPullParser parser) throws XmlPullParserException, IOException {


parser.require(XmlPullParser.START_TAG, ns, "link");
String link = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "link");
return link;
}

private String readTitle(XmlPullParser parser) throws XmlPullParserException, IOException {


parser.require(XmlPullParser.START_TAG, ns, "title");
String title = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "title");
return title;
}

// For the tags title and link, extract their text values.
private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return result;
}
}

layout/fagment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
android:visibility="gone"
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>

<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />

</RelativeLayout>

layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="@+id/fragment_container"
android:layout_height="fill_parent" />

layout_items.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/itemTitle"
android:textSize="18dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.madlab.rssfeed"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
/>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
<service android:name=".RssService" />
</application>

</manifest>
OUTPUT:

RESULT:

Thus the application that makes use of RSS Feed was executed successfully.

You might also like