Search code examples
javaandroidxmlrssdom4j

DOM4j doesn't work on Android


I try to use DOM4j to phase a XML RSS feed on Android. I do add the dom4j.jar to lib\ and the user permission.

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

I also compile this source code in pure Java project, and it works! I'm confused if there is any restrictions on Android(if yes, what methods can I use), or only I made some mistakes.

Parts of Logcat:

03-15 23:27:10.611: W/System.err(10453): org.dom4j.DocumentException: Couldn't open http://tw.news.yahoo.com/rss/ Nested exception: Couldn't open http://tw.news.yahoo.com/rss/
03-15 23:27:10.621: W/System.err(10453):    at org.dom4j.io.SAXReader.read(SAXReader.java:484)
03-15 23:27:10.621: W/System.err(10453):    at org.dom4j.io.SAXReader.read(SAXReader.java:291)
03-15 23:27:10.621: W/System.err(10453):    at tw.danny.rss_dom4j.RSSDom4jMainActivity.onCreate(RSSDom4jMainActivity.java:28)
03-15 23:27:10.621: W/System.err(10453):    at android.app.Activity.performCreate(Activity.java:5206)
03-15 23:27:10.626: W/System.err(10453):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
03-15 23:27:10.626: W/System.err(10453):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
...

Here is onCreate function in main activity:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rssdom4j_main);

    try {
        URL url = new URL("http://tw.news.yahoo.com/rss/");
        SAXReader saxReader = new SAXReader();
        //?
        Document document = saxReader.read(url);
        Element channel = (Element) document.getRootElement().element(
                "channel");
        for (Iterator i = channel.elementIterator("item"); i.hasNext();) {
            Element element = (Element) i.next();
            System.out.println("title: " + element.elementText("title"));

            String descrip_original = element.elementText("description");
            if (descrip_original.startsWith("<p>")) {
                // address html-like layout
                System.out.println(descrip_original.substring(
                        descrip_original.indexOf("</a>") + 4,
                        descrip_original.length() - 1));

            }

            System.out.println("link: " + element.elementText("link"));
            System.out
                    .println("pubDate: " + element.elementText("pubDate"));

            System.out.println();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Problems may occur in this line:

SAXReader saxReader = new SAXReader();

Solution

  • You're accessing the internet on the main UI thread. That's a no-no since Android 4.0.x. Place the code that accesses the network in an AsyncTask or spin up a new Thread for it.

    new Thread( new Runnable() {
        @Override
        public void run() {
            try {
                URL url = new URL("http://tw.news.yahoo.com/rss/");
                SAXReader saxReader = new SAXReader();
    
                Document document = saxReader.read(url);
                Element channel = (Element) document.getRootElement().element(
                        "channel");
                for (Iterator i = channel.elementIterator("item"); i.hasNext();) {
                    Element element = (Element) i.next();
                    System.out.println("title: " + element.elementText("title"));
    
                    String descrip_original = element.elementText("description");
                    if (descrip_original.startsWith("<p>")) {
                        // address html-like layout
                        System.out.println(descrip_original.substring(
                                descrip_original.indexOf("</a>") + 4,
                                descrip_original.length() - 1));
    
                    }
    
                    System.out.println("link: " + element.elementText("link"));
                    System.out
                            .println("pubDate: " + element.elementText("pubDate"));
    
                    System.out.println();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    } ).start();