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


    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();
            }
  • 相关阅读:
    Grid 拾遗
    WPF 依赖属性
    升级cordova
    oracle比较一行的最大值或最小值
    nginx完全关闭log
    hive计算周一的日期
    查看linux文件目录的大小和文件夹包含的文件数
    linux设置服务器时间同步
    oracle取出多个字段列中的最大值和最小值
    oracle SQL语句取本周本月本年的数据
  • 原文地址:https://www.cnblogs.com/yejiurui/p/2829661.html
Copyright © 2020-2023  润新知