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 [Link] the lowest android version(Android 2.2) and select next
4) Enter the package [Link] name must be two word separated by comma and click finish
5)Go to package explorer in the left hand [Link] our project.
6)Go to res folder and select [Link] click and edit the [Link] 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.
//[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link]);
if (savedInstanceState == null) {
addRssFragment();
}
}
private void addRssFragment() {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = [Link]();
RssFragment fragment = new RssFragment();
[Link]([Link].fragment_container, fragment);
[Link]();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
[Link](outState);
[Link]("fragment_added", true);
}
}
//[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class RssFragment extends Fragment implements OnItemClickListener {
private ProgressBar progressBar;
private ListView listView;
private View view;
@Override
public void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
if (view == null) {
view = [Link]([Link].fragment_layout, container, false);
progressBar = (ProgressBar) [Link]([Link]);
listView = (ListView) [Link]([Link]);
[Link](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) [Link]();
[Link](view);
}
return view;
}
private void startService() {
Intent intent = new Intent(getActivity(), [Link]);
[Link]([Link], 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>) [Link]([Link]);
if (items != null) {
RssAdapter adapter = new RssAdapter(getActivity(), items);
[Link](adapter);
} else {
[Link](getActivity(), "An error occured while downloading the rss feed.",
Toast.LENGTH_LONG).show();
}
[Link]([Link]);
[Link]([Link]);
};
};
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
RssAdapter adapter = (RssAdapter) [Link]();
RssItem item = (RssItem) [Link](position);
Uri uri = [Link]([Link]());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
}
//[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
public class RssService extends IntentService {
private static final String RSS_LINK = "[Link]
public static final String ITEMS = "items";
public static final String RECEIVER = "receiver";
public RssService() {
super("RssService");
}
@Override
protected void onHandleIntent(Intent intent) {
// Log.d([Link], "Service started");
List<RssItem> rssItems = null;
try {
PcWorldRssParser parser = new PcWorldRssParser();
rssItems = [Link](getInputStream(RSS_LINK));
} catch (XmlPullParserException e) {
Log.w([Link](), e);
} catch (IOException e) {
Log.w([Link](), e);
}
Bundle bundle = new Bundle();
[Link](ITEMS, (Serializable) rssItems);
ResultReceiver receiver = [Link](RECEIVER);
[Link](0, bundle);
}
public InputStream getInputStream(String link) {
try {
URL url = new URL(link);
return [Link]().getInputStream();
} catch (IOException e) {
// Log.w([Link], "Exception while retrieving the input stream", e);
return null;
}
}
}
//[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
public class RssAdapter extends BaseAdapter {
private final List<RssItem> items;
private final Context context;
public RssAdapter(Context context, List<RssItem> items) {
[Link] = items;
[Link] = context;
}
@Override
public int getCount() {
return [Link]();
}
@Override
public Object getItem(int position) {
return [Link](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 = [Link](context, [Link].rss_item, null);
holder = new ViewHolder();
[Link] = (TextView) [Link]([Link]);
[Link](holder);
} else {
holder = (ViewHolder) [Link]();
}
[Link]([Link](position).getTitle());
return convertView;
}
static class ViewHolder {
TextView itemTitle;
}
//[Link]
package [Link];
public class RssItem {
private final String title;
private final String link;
public RssItem(String title, String link) {
[Link] = title;
[Link] = link;
}
public String getTitle() {
return title;
}
public String getLink() {
return link;
}
}
//[Link]
package [Link];
import [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
import [Link];
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 = [Link]();
[Link](XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
[Link](inputStream, null);
[Link]();
return readFeed(parser);
} finally {
[Link]();
}
}
private List<RssItem> readFeed(XmlPullParser parser) throws XmlPullParserException,
IOException {
[Link](XmlPullParser.START_TAG, null, "rss");
String title = null;
String link = null;
List<RssItem> items = new ArrayList<RssItem>();
while ([Link]() != XmlPullParser.END_DOCUMENT) {
if ([Link]() != XmlPullParser.START_TAG) {
continue;
}
String name = [Link]();
if ([Link]("title")) {
title = readTitle(parser);
} else if ([Link]("link")) {
link = readLink(parser);
}
if (title != null && link != null) {
RssItem item = new RssItem(title, link);
[Link](item);
title = null;
link = null;
}
}
return items;
}
private String readLink(XmlPullParser parser) throws XmlPullParserException, IOException {
[Link](XmlPullParser.START_TAG, ns, "link");
String link = readText(parser);
[Link](XmlPullParser.END_TAG, ns, "link");
return link;
}
private String readTitle(XmlPullParser parser) throws XmlPullParserException, IOException {
[Link](XmlPullParser.START_TAG, ns, "title");
String title = readText(parser);
[Link](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 ([Link]() == [Link]) {
result = [Link]();
[Link]();
}
return result;
}
}
layout/fagment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
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/[Link]
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="[Link]
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="[Link]
android:id="@+id/itemTitle"
android:textSize="18dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
[Link]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
package="[Link]"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
/>
<uses-permission android:name="[Link]" />
<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="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
<service android:name=".RssService" />
</application>
</manifest>
OUTPUT:
RESULT:
Thus the application that makes use of RSS Feed was executed successfully.