-1

I am trying to implement very simple RSS parser using this RSS parsing library According to the implementation all i have to do is paste the sample code provided, once I import the code as a library.

Steps I have taken:

  1. Download the zip file from the link provided above.
  2. Create new project from existing code
  3. Add the added project as a library to my project.
  4. Add the example code from the link provided above and modify it with try and catch statements and add textview as the output for the title from the rss feed.
  5. Try and run the application. It crashes.

Is it because the library is quite old? Have I done something incorrectly? Any help greatly appreciated.


CODE:

package com.example.rssparser;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import org.xml.sax.SAXException;

import nl.matshofman.saxrssreader.RssFeed;
import nl.matshofman.saxrssreader.RssItem;
import nl.matshofman.saxrssreader.RssReader;
import android.app.Activity;
import android.os.Bundle;
//import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
TextView rss_feed_data;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    URL url;
    try {
        url = new URL("http://wwwnc.cdc.gov/travel/rss/notices.xml");

        RssFeed feed;
        try {
            feed = RssReader.read(url);
            ArrayList<RssItem> rssItems = feed.getRssItems();
            for(RssItem rssItem : rssItems) {
                //Log.i("RSS Reader", rssItem.getTitle());
                rss_feed_data = (TextView) findViewById(R.id.rss_feed_data);
                rss_feed_data.setText(rssItem.getTitle());

            }
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
   }

}

LOGCAT OUTPUT:

11-20 04:01:36.901: E/Trace(16331): error opening trace file: No such file or directory (2)
11-20 04:01:37.088: W/dalvikvm(16331): threadid=1: thread exiting with uncaught   exception (group=0x41269908)
11-20 04:01:37.138: E/AndroidRuntime(16331): FATAL EXCEPTION: main
11-20 04:01:37.138: E/AndroidRuntime(16331): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rssparser/com.example.rssparser.MainActivity}:  android.os.NetworkOnMainThreadException
11-20 04:01:37.138: E/AndroidRuntime(16331):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2234)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at android.app.ActivityThread.access$600(ActivityThread.java:181)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1332)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at android.os.Looper.loop(Looper.java:153)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at android.app.ActivityThread.main(ActivityThread.java:5037)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at java.lang.reflect.Method.invokeNative(Native Method)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at java.lang.reflect.Method.invoke(Method.java:511)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:588)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at dalvik.system.NativeStart.main(Native Method)
11-20 04:01:37.138: E/AndroidRuntime(16331): Caused by: android.os.NetworkOnMainThreadException
11-20 04:01:37.138: E/AndroidRuntime(16331):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1125)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:292)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:185)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at    java.net.URL.openStream(URL.java:462)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at nl.matshofman.saxrssreader.RssReader.read(RssReader.java:35)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at com.example.rssparser.MainActivity.onCreate(MainActivity.java:29)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at android.app.Activity.performCreate(Activity.java:5020)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-20 04:01:37.138: E/AndroidRuntime(16331):    at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
11-20 04:01:37.138: E/AndroidRuntime(16331):    ... 11 more

Updated Code

package com.example.rssparser;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import nl.matshofman.saxrssreader.RssFeed;
import nl.matshofman.saxrssreader.RssItem;
import nl.matshofman.saxrssreader.RssReader;

import org.xml.sax.SAXException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;
//import android.util.Log;

public class MainActivity extends Activity {
TextView rss_feed_data;
String ur = "http://wwwnc.cdc.gov/travel/rss/notices.xml";
ProgressDialog pDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rss_feed_data = (TextView) findViewById(R.id.rss_feed_data);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
     new XmlParsing(ur).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new String[]{null});
else
     new XmlParsing(ur).execute(new String[]{null});

}

public class XmlParsing extends AsyncTask<String, String, ArrayList<RssItem>> {

    // variables passed in:
    String urls;
    //  constructor
    public XmlParsing(String urls) {
        this.urls = urls;
    }

    @Override
    protected void onPreExecute() {
        pDialog = ProgressDialog.show(MainActivity.this, "Fetching Details..", "Please wait...", true);
    }


    @Override
    protected ArrayList<RssItem> doInBackground(String... params) {
        // TODO Auto-generated method stub


        URL url;
        try {
            url = new URL(urls);
            RssFeed feed;
            try {
                feed = RssReader.read(url);
                ArrayList<RssItem> rssItems = feed.getRssItems();
                return rssItems;
                } catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace(); }
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
    }

    protected void onPostExecute(String result, ArrayList<RssItem> rssItems) {
        // Now we have your JSONObject, play around with it.
        if (pDialog.isShowing())
            pDialog.dismiss();

        for(RssItem rssItem : rssItems) {
                //Log.i("RSS Reader", rssItem.getTitle());

                rss_feed_data.setText(rssItem.getTitle());
        }

    }
}
}

LOGCAT Output

11-20 05:36:50.439: I/SurfaceTextureClient(17281): [STC::queueBuffer] this:0x516a20a8, api:1, last queue time elapsed :9510 ms
11-20 05:36:50.441: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:10.505000, fps:6.282219
11-20 05:36:50.473: I/SurfaceTextureClient(17281): [STC::queueBuffer] this:0x51655180, api:1, last queue time elapsed :131816 ms
11-20 05:36:50.494: I/SurfaceTextureClient(17281): [0x51655180] frames:1, duration:131.815994, fps:0.007586
11-20 05:36:51.426: I/SurfaceTextureClient(17281): [0x516a20a8] frames:45, duration:1.016000, fps:44.289616
11-20 05:36:52.437: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.010000, fps:65.308807
11-20 05:36:53.449: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.012000, fps:65.186592
11-20 05:36:54.462: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.012000, fps:65.197708
11-20 05:36:55.474: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.012000, fps:65.203377
11-20 05:36:56.486: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.012000, fps:65.198250
11-20 05:36:57.498: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.012000, fps:65.211411
11-20 05:36:58.509: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.010000, fps:65.283676
11-20 05:36:59.520: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66,  duration:1.010000, fps:65.318069
11-20 05:37:00.530: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.010000, fps:65.322647
11-20 05:37:01.542: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.240547
11-20 05:37:02.556: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.244469
11-20 05:37:03.565: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.245308
11-20 05:37:04.577: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.252708
11-20 05:37:05.588: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.248543
11-20 05:37:06.600: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.246178
11-20 05:37:07.611: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.253662
11-20 05:37:08.622: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.253265
11-20 05:37:09.634: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.248100
11-20 05:37:10.646: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.222153
11-20 05:37:11.656: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.010000, fps:65.313301
11-20 05:37:12.667: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.010000, fps:65.318176
11-20 05:37:13.678: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.267540
11-20 05:37:14.689: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.262505
11-20 05:37:15.701: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.258896
11-20 05:37:16.712: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.275986
11-20 05:37:17.723: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.265404
11-20 05:37:18.734: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.277664
11-20 05:37:19.745: I/SurfaceTextureClient(17281): [0x516a20a8] frames:66, duration:1.011000, fps:65.250298
f78xd23
  • 117
  • 2
  • 15
  • If it crashes, could you post your LogCat? – Andrew T. Nov 20 '13 at 03:31
  • I've added the logcat. I found this [probable solution] (http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) , but I'm not sure how to implement it into my code. I'll post what I've done with it. – f78xd23 Nov 20 '13 at 04:20

1 Answers1

1

This exception is thrown when an application attempts to perform a networking operation on its main thread. Run your code in AsyncTask:

public class MainActivity extends Activity {
TextView rss_feed_data;
String ur = "http://wwwnc.cdc.gov/travel/rss/notices.xml";
ProgressDialog pDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    rss_feed_data = (TextView) findViewById(R.id.rss_feed_data);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
         new XmlParsing(ur).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new String[]{null});
    else
         new XmlParsing(ur).execute(new String[]{null});

}

public class XmlParsing extends AsyncTask<String, String, ArrayList<RssItem>> {

        // variables passed in:
        String urls;
        //  constructor
        public XmlParsing(String urls) {
            this.urls = urls;
        }

        @Override
        protected void onPreExecute() {
            pDialog = ProgressDialog.show(NetActivity.this, "Fetching Details..", "Please wait...", true);
        }


        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub


            URL url;
        try {
            url = new URL();

            RssFeed feed;
            try {
                feed = RssReader.read(url);
                ArrayList<RssItem> rssItems = feed.getRssItems();
                return rssItems;
                }
            } catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            // Now we have your JSONObject, play around with it.
            if (pDialog.isShowing())
                pDialog.dismiss();

            for(RssItem rssItem : rssItems) {
                    //Log.i("RSS Reader", rssItem.getTitle());

                    rss_feed_data.setText(rssItem.getTitle());
        }

    }
Hariharan
  • 24,741
  • 6
  • 50
  • 54
  • Hey, I am not sure if the code was meant to work by just copy and paste, but if so it had a lot of errors. I fixed most of them, by eclipse recommendations, but one of the errors no recommended solution works. It is the rssItems at the end, on hover over it states (rssItems cannot be resolved to a variable). I have updated the code. I understand that it cannot see the rssItems declared above, but can't seem to work out how to fix it. – f78xd23 Nov 20 '13 at 05:01
  • @paddysal use `ArrayList rssItems;` in global. – Hariharan Nov 20 '13 at 05:31
  • Yeah I worked it out in the end, though fetching the data is taking forever. I think I'll need to use some different method. It's been 3 minutes and it's still fetching... I have added logcat, it seems to be doing one thing over and over again. – f78xd23 Nov 20 '13 at 05:35
  • @paddysal did you get the result. – Hariharan Nov 20 '13 at 06:27
  • nope it didn't return anything, it just kept fetching forever and logcat was full of the messages like the last part of my post above. I have no idea how to make it work. I have implemented the code you have on your web blog, it works with the url from androidhive, but didn't try modifying it to work with the rss feed url i need data from. Will give it a go later on. – f78xd23 Nov 20 '13 at 06:41