0

我尝试使用 DOM4j 在 Android 上调整 XML RSS 提要。我确实将 dom4j.jar 添加到 lib\ 和用户权限。

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

我也在纯Java项目中编译了这个源代码,它可以工作!我很困惑Android是否有任何限制(如果有,我可以使用什么方法),或者只是我犯了一些错误。

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)
...

这是主要活动中的 onCreate 函数:

    @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();
    }
}

这一行可能会出现问题:

SAXReader saxReader = new SAXReader();
4

1 回答 1

0

您正在主 UI 线程上访问互联网。这是自 Android 4.0.x 以来的禁忌。将访问网络的代码放在 AsyncTask 中或为其创建一个新线程。

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();
于 2013-03-15T16:10:24.943 回答