android 读取网络 xml 数据

private void initData() {
    final String PATH = "http://www.oschina.net/news/rss";
    new Thread() {
 
        @Override
        public void run() {
            try {
                URL url = new URL(PATH);
                HttpURLConnection huc = (HttpURLConnection) url.openConnection();
                InputStream is = huc.getInputStream();
                XmlPullParser xpp = Xml.newPullParser();
                xpp.setInput(is, "utf-8");
                int eventType = xpp.getEventType();
                while (eventType != XmlPullParser.END_DOCUMENT) {
                    switch (xpp.getEventType()) {
                    case XmlPullParser.START_DOCUMENT:
                        Log.d("oldfeel", "start document");
                        break;
                    case XmlPullParser.START_TAG:
                        String name = xpp.getName();
                        Log.d("oldfeel", name);
                        if (name.equals("description")) {
                            Log.d("oldfeel", "next text " + xpp.nextText());
                        }
                        break;
                    default:
                        break;
                    }
                    eventType = xpp.next(); // 读取下一个tag
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            super.run();
        }
 
    }.start();
}

你可能感兴趣的:(android,xml)