现在的位置: 首页 > 综合 > 正文

Android中xml解析–实现软件升级功能

2012年02月09日 ⁄ 综合 ⁄ 共 1868字 ⁄ 字号 评论关闭

android中自带有pull解析器,所以我们一般都使用pull来解析。这里解析一个最简单的软件升级的xml文件,通过pull解析,获取到软件的版本号,和描述,还有下载地址,实现软件的更新操作。

使用最常用的pull解析器来实现xml解析,实现软件的升级功能!

1.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<info>
    <version>2.0</version>
    <description>有新的版本了,赶快来下载吧!</description>
    <path>http://xxx.xxxx.xx</path>
</info>

2.解析xml的业务bean

public class UpdateInfo {
    private String version;
    private String description;
    private String path;
    public String getVersion() {
        return version;
    }
    public void setVersion(String version) {
        this.version = version;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getPath() {
        return path;
    }
    public void setPath(String path) {
        this.path = path;
    }
}

3.开始解析xml文件

/**
 * 获取更新信息
 * 
 * @author piao
 * 
 */
public class UpdateInfoProvider {
    //解析xml文件
    public  static UpdateInfo getUpdateInfo(InputStream is) {
        XmlPullParser parser = Xml.newPullParser();
        UpdateInfo info = new UpdateInfo();
        // 初始化解析器
        try {
            parser.setInput(is, "utf-8");
            int type = parser.getEventType();

            while (type != XmlPullParser.END_DOCUMENT) {
                switch (type) {
                case XmlPullParser.START_TAG:
                    if ("version".equals(parser.getName())) {
                        String version = parser.nextText();
                        info.setVersion(version);
                    } else if ("description".equals(parser.getName())) {
                        String description = parser.nextText();
                        info.setDescription(description);
                    } else if ("path".equals(parser.getName())) {
                        String path = parser.nextText();
                        info.setPath(path);
                    }
                    break;
                }
                type = parser.next();
            }
            return info;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }
}

4.接受xml文件返回的数据

try {
            URL url=new URL(xxx);
            HttpURLConnection conn=url.openConnection();
            conn.setRequestMethod("GET");
            //连接超时时间
            conn.setConnectTimeout(5000);
            int code = conn.getResponseCode();
            if(code==200){
                InputStream is=conn.getInputStream();
                UpdateInfo updateInfo=new UpdateInfo();
                updateInfo=testxml.getUpdateInfo(is);
                if(updateInfo!=null){
                    //解析成功
                }else{
                    //解析失败
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
【上篇】
【下篇】

抱歉!评论已关闭.