2

I followed a simple tutorial (http://www.cse.nd.edu/courses/cse40814/www/RSS_Android.pdf) to read RSS feed from a given URL into a listView. I have added the INTERNET permission and the code is error free in Eclipse but it will not show any RSS feed when launched on a device or emulator. I can't make the code anymore simpler, and the feed I am using is stable feed from www.nba.com : http://www.nba.com/rss/nba_rss.xml though i have tested it on several RSS feeds and with still no feed showing.

Any ideas guys?

Main.java

package com.android.simplerssreader;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.android.simplerssreader.data.RssItem;
import com.android.simplerssreader.util.RssReader;

public class Main extends Activity {

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

    try {

        RssReader rssReader = new RssReader(
                "http://www.nba.com/rss/nba_rss.xml");
        ListView Items = (ListView) findViewById(R.id.listView1);

        ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(this,
                android.R.layout.simple_list_item_1, rssReader.getItems());

        Items.setAdapter(adapter);
        Items.setOnItemClickListener(new ListListener(rssReader.getItems(),
                this));

    } catch (Exception e) {
        Log.e("SimpleRssReader", e.getMessage());
    }

}
 }

ListListener.java

package com.android.simplerssreader;

import java.util.List;

import com.android.simplerssreader.data.RssItem;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class ListListener implements OnItemClickListener {

List<RssItem> listItems;
Activity activity;

public ListListener(List<RssItem> listItems, Activity activity) {
    this.listItems = listItems;
    this.activity = activity;
}

public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {

    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(listItems.get(pos).getLink()));
    activity.startActivity(i);

}

}

RssItem.java

package com.android.simplerssreader.data;

public class RssItem {
private String title;
private String link;
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}
public String getLink() {
    return link;
}
public void setLink(String link) {
    this.link = link;
}

}

RssParseHandler.java

package com.android.simplerssreader.util;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.android.simplerssreader.data.RssItem;

public class RssParseHandler extends DefaultHandler {

private List<RssItem> rssItems;

private RssItem currentItem;
private boolean parsingTitle;
private boolean parsingLink;

public RssParseHandler() {
    rssItems = new ArrayList<RssItem>();
}

public List<RssItem> getItems() {
    return rssItems;
}

@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {
    if ("content-item".equals(qName)) {
        currentItem = new RssItem();
    } else if ("title".equals(qName)) {
        parsingTitle = true;
    } else if ("url".equals(qName)) {
        parsingLink = true;
    }
}

@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {
    if ("content-item".equals(qName)) {
        rssItems.add(currentItem);
        currentItem = null;
    } else if ("title".equals(qName)) {
        parsingTitle = false;
    } else if ("url".equals(qName)) {
        parsingLink = false;
    }
}

@Override
public void characters(char[] ch, int start, int length)
        throws SAXException {
    if (parsingTitle) {
        if (currentItem != null)
            currentItem.setTitle(new String(ch, start, length));
    } else if (parsingLink) {
        if (currentItem != null) {
            currentItem.setLink(new String(ch, start, length));
            parsingLink = false;
        }
    }
}

}

RssReader.java

package com.android.simplerssreader.util;

import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import com.android.simplerssreader.data.RssItem;

public class RssReader {

private String rssUrl;

public RssReader(String rssUrl) {

    this.rssUrl = rssUrl;
}

public List<RssItem> getItems() throws Exception {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser = factory.newSAXParser();

    RssParseHandler handler = new RssParseHandler();
    saxParser.parse(rssUrl, handler);
    return handler.getItems();
}
}

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:background="#000000"
android:orientation="vertical" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:cacheColorHint="#FFA500"
    android:scrollingCache="false"
    android:textColor="#ADD8E6" >
</ListView>

</LinearLayout>

AndroidManifest.xml

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

<uses-permission android:name="android.permission.INTERNET" />

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="16" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.android.simplerssreader.Main"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

LogCat

01-04 16:22:16.171: E/SimpleRssReader(8685): Couldn't open http://www.nba.com/rss/nba_rss.xml

AndroidNewb
  • 441
  • 3
  • 6
  • 14

1 Answers1

1

replace

Log.e("SimpleRssReader", e.getMessage());

with

Log.e("SimpleRssReader", e.getMessage(), e);

you lose the stack trace information.

BTW, your error is that you can't access the network in Android when you are inside the UI Thread (after HoneyComb) : How to fix android.os.NetworkOnMainThreadException?

Community
  • 1
  • 1
Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • Snicolas I want to start by saying thank you for lending your expertise. I changed that line and I began getting the proper error: "android.os.NetworkOnMainThreadException". I took a look at the link you provided but after giving it look, I'm not totally sure how to implement this in my Main.java. I know I can sub in my try block but when i extend AsyncTask what parameters will i need? And I don't really need to declare "private Exception exception;" and I do not have this method "new RetreiveFeedTask().execute(urlToRssFeed);" right? Could you show me how to implement this in my Main.java? – AndroidNewb Jan 04 '13 at 23:14
  • That's actually a long topic on Android. First try to use an AsyncTask and make your code working. In that case define a simple AsyncTask>, all you have to do is to call your RssReader inside the doInBackGround method of your asynctask, and call execute on your asynctask passing it the url. Take your time, look for a few examples, you will make it. But, you should keep in mind that this technique has some drawbacks, and when you are more confortable with android, have a look a robospice, it's a lib on github that provides a more robust approach to networking. – Snicolas Jan 05 '13 at 08:29
  • this may be helpful : http://www.itcuties.com/android/android-asynctask-rss-reader/ – gray Apr 02 '14 at 13:27