zoukankan      html  css  js  c++  java
  • Android网络编程之一个Android下菜单系统模块的实现(客户端—更新菜单)

    更新菜单的客户端编程也与更新桌号完全一样,在UpdateActivity中把updateMenus()方法编写完成即可:

        private void updateMenus() {
            String urlString = HttpUtil.BASE_URL + "servlet/UpdateMenuServlet";
            InputStream is = null;
            URLConnection conn = null;
            
            try {
                // 实例化目标servlet的地址并取得连接的输入流
                URL url = new URL(urlString);
                conn = url.openConnection();
                is = conn.getInputStream();
                
                // 准备读取xml文件所需的所有类实例
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = dbf.newDocumentBuilder();
                Document doc = builder.parse(is);
                // 获取所有table节点装入列表
                NodeList nodeList = doc.getElementsByTagName("menu");
                
                // 获得ContentResolver在更新前删除旧内容
                ContentResolver resolver = this.getContentResolver();
                Uri menuProviderURI = Menus.CONTENT_URI;
                resolver.delete(menuProviderURI, null, null);
            
                // 从xml中提取数据并用ContentProvider插入sqlite表中
                for (int i = 0; i < nodeList.getLength(); i++) {
                    Element e = (Element) nodeList.item(i);
                    ContentValues values = new ContentValues();
                    values.put("_id", e.getElementsByTagName("id").item(0).getFirstChild().getNodeValue());
                    values.put("price", e.getElementsByTagName("price").item(0).getFirstChild().getNodeValue());
                    values.put("typeId", e.getElementsByTagName("typeId").item(0).getFirstChild().getNodeValue());
                    values.put("name", e.getElementsByTagName("name").item(0).getFirstChild().getNodeValue());
                    values.put("pic", e.getElementsByTagName("pic").item(0).getFirstChild().getNodeValue());
                    values.put("remark", e.getElementsByTagName("remark").item(0).getFirstChild().getNodeValue());
    
                    resolver.insert(menuProviderURI, values);
                }
                Toast.makeText(UpdateActivity.this, "更新菜单成功", Toast.LENGTH_SHORT).show();
                
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (is != null) {
                        is.close();    
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    View Code

    同样拿“点餐”模块试验,我们先在服务器端menutbl中新增一条数据:第6条价格为100的一盆菜

    然后执行更新菜单功能,再看sqlite中与菜品下拉列表中是否更新:

    sqlite中的menutbl表:

    至此,更新功能模块编写完毕,本项目的三个最主要功能暂时编写完成了。

  • 相关阅读:
    微服务热部署,jrebel激活破解
    微服务改代码热部署
    MYBATIS框架的映射关系
    postgrepsql数据库保留两位有效数字
    It's likely that neither a Result Type nor a Result Map was specified
    idea启动微服务项目报错
    微服务自动加载common包
    No function matches the given name and argument types. You might need to add explicit type casts postgrepsql数据库
    "Connect to localhost:9000 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect",
    链表的排序 (选择和冒泡)
  • 原文地址:https://www.cnblogs.com/moka/p/3088410.html
Copyright © 2011-2022 走看看