zoukankan      html  css  js  c++  java
  • 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();
            }
  • 相关阅读:
    10种 分布式ID生成方式(新增MongoDB的ObjectId)
    Spring核心接口Ordered的实现及应用 (动态切换数据源时候用到)
    No module named 'Crypto' 解决方案
    使用Anaconda管理多个版本的Python环境
    深入浅出Blazor webassembly 之API服务端保护
    [转载]HTTPS 是如何保护你的安全的
    [转载]api接口token的生成和应用
    深入浅出Blazor webassembly之HttpClient使用
    深入浅出Blazor webassembly之自定义Input组件
    深入浅出Blazor webassembly之EditForm
  • 原文地址:https://www.cnblogs.com/yejiurui/p/2829661.html
Copyright © 2011-2022 走看看