0

我是 android 的新手,我正在尝试从http://www.astrology.com/horoscopes/daily-horoscope.rss解析 xml 数据,我从 How can I parse xml from url in android? 在这里,我使用了sherLock库,我也在manifest中设置了互联网权限,但仍然无法抓取它,这里是相同的代码,只是稍微修改一下,

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /* Create a new layout to display the view */
    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(1);

    /* Create a new textview array to display the results */
    TextView name[];
    //TextView website[];
    //TextView category[];

    try {

        URL url = new URL("http://www.astrology.com/horoscopes/daily-horoscope.rss");
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new InputSource(url.openStream()));
        doc.getDocumentElement().normalize();

        NodeList nodeList = doc.getElementsByTagName("item");

        /** Assign textview array lenght by arraylist size */
        name = new TextView[nodeList.getLength()];
       // website = new TextView[nodeList.getLength()];
        //category = new TextView[nodeList.getLength()];

        for (int i = 0; i < nodeList.getLength(); i++) {

            Node node = nodeList.item(i);

            name[i] = new TextView(this);
            //website[i] = new TextView(this);
            //category[i] = new TextView(this);

            Element fstElmnt = (Element) node;
            NodeList nameList = ((Document) fstElmnt).getElementsByTagName("title");
            Element nameElement = (Element) nameList.item(0);
            nameList = ((Node) nameElement).getChildNodes();
            name[i].setText("Name = " + ((Node) nameList.item(0)).getNodeValue());

            //category[i].setText("Website Category = " + ((org.w3c.dom.Element) websiteElement).getAttribute("category"));

            layout.addView(name[i]);
            //layout.addView(website[i]);
            //layout.addView(category[i]);
        }
    } catch (Exception e) {
        System.out.println("XML Pasing Excpetion = " + e);
    }

    /** Set the layout view to display */
    setContentView(layout);

}

请记住,我正在从 SherlockActivity 扩展我的 MainActivity 类,当我执行代码时,只出现操作栏!

4

0 回答 0