当前位置:操作系统 > 安卓/Android >>

从网络读取数据并动态的显示在ListView中

 

这两天写了个小程序,使用了从网络读取xml数据,并显示在ListView中。

这里面有几个关键点:

从网络读取数据

SAX解析xml

异步填充ListView

先看下截图:

 

\

 

非常简单的界面哈

 

为了方便,我再自己的服务器上,放了一个xml文件,其内容主要是:

 

<?xml version="1.0"?> 

<products> 

    <product> 

        <price>100</price> 

        <name>android dev</name> 

        <image src="image/android1.png"/> 

    </product> 

    <product> 

        <price>100</price> 

        <name>androiddev2</name> 

        <image src="image/android1.png"/> 

    </product> 

<!-- 接着重复下去.... --> 

</products> 

 

该程序,首先用一个AsyncTask新启一个线程,来下载和解析xml;和主线程通过Handler对象来通讯。

 

 

 

下载XML文件

 

通过HTTPGet来下载xml。这时apache提供的包,需要首先包含如下包:

 

 

import org.apache.http.HttpEntity; 

import org.apache.http.HttpResponse; 

import org.apache.http.HttpStatus; 

import org.apache.http.client.HttpClient; 

import org.apache.http.client.methods.HttpGet; 

import org.apache.http.impl.client.DefaultHttpClient; 

 

代码如下:(使用Get方法)

 

protected String doInBackground(String... params) { 

    HttpGet httpRequest = new HttpGet(params[0]); //从url 创建一个HttpGet对象 

    HttpClient httpclient = new DefaultHttpClient(); 

     

    //mShowHtml.setText(""); 

    try { 

        HttpResponse httpResponse = httpclient.execute(httpRequest); 

          

        if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){//获取http的返回值代码 

            HttpEntity entitiy = httpResponse.getEntity(); 

             

            InputStream in = entitiy.getContent();//获得内容 

            //解析xml,下面详述 

            InputSource source = new InputSource(in); 

             

            SAXParserFactory sax = SAXParserFactory.newInstance(); 

            XMLReader xmlReader = sax.newSAXParser().getXMLReader(); 

             

            xmlReader.setContentHandler(new ProductHandler()); 

             

            xmlReader.parse(source); 

             

        } 

        else { 

            //return "请求失败!"; 

            //mShowHtml.setText("请求失败"); 

            //Message mymsg = mMainHandler.obtainMessage(); 

            //mymsg.obj = "请求失败"; 

            //mMainHandler.sendMessage(mymsg); 

        } 

     

    }catch(IOException e){ 

        e.printStackTrace(); 

    }catch(SAXException e) { 

        e.printStackTrace(); 

    }catch(ParserConfigurationException e) { 

        e.printStackTrace(); 

    } 

    return null; 

 

 

解析XML

使用SAX的解析方法,先包含必须得包

 

import org.xml.sax.InputSource; 

import org.xml.sax.SAXException; 

import org.xml.sax.XMLReader; 

import org.xml.sax.helpers.DefaultHandler; 

 

解析的代码,正如上面所示:

 

                

//InputSource是解析源,可以通过一个InputStream创建 

urce source = new InputSource(in);                   

SAXParserFactory sax = SAXParserFactory.newInstance(); 

der xmlReader = sax.newSAXParser().getXMLReader();                            

xmlReader.setContentHandler(new ProductHandler());//ProductHandler是解析的句柄                         

der.parse(source); 

SAX主要使用ContentHandler接口来传递解析好得数据,不过,更经常使用的是DefaultHandler

 

 

class ProductHandler extends DefaultHandler { //从DefaultHandler继承即可 

    private ProductInfo curProduct; 

    private String content; 

    //解析到一个标签时调用 

    public void startElement(String uri, String localName, String name, org.xml.sax.Attributes attributes) throws SAXException { 

        if(localName.equals("product")) {//遇到product标签,进行解析 

            curP

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,