当前位置: 首页 > news >正文

关于安卓通过webservice访问数据库问题

问题描述:

访问数据库时,手机能增删数据库的数据就是显示不了数据库的里的数据不知道是哪里的问题,用的HTTP
这是我webservice中的产看所有信息的方法:

public List<string> selectAllCargoInfor()
        {
            List<string> list = new List<string>();

            try
            {
                string sql = "select * from C";
                SqlCommand cmd = new SqlCommand(sql,sqlCon);
                SqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    //将结果集信息添加到返回向量中
                    list.Add(reader[0].ToString());
                    list.Add(reader[1].ToString());
                    list.Add(reader[2].ToString());

                }

                reader.Close();
                cmd.Dispose();

            }
            catch(Exception)
            {

            }
            return list;
        }

接下来是安卓端的:
这个是MainActivity中的设置LISTVIEW的方法

private void setListView() {
        listView.setVisibility(View.VISIBLE);
        List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
        list = dbUtil.getAllInfo();
        adapter = new SimpleAdapter(MainActivity.this, list, R.layout.adapter_item, 
        new String[] { "Cno", "Cname", "Cnum" }, 
        new int[] { R.id.txt_Cno, R.id.txt_Cname, R.id.txt_Cnum });
        listView.setAdapter(adapter);
    }

这个是操作类:

public List<HashMap<String, String>> getAllInfo() {
        List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();

        arrayList.clear();
        brrayList.clear();
        crrayList.clear();

        new Thread(new Runnable() {
            
            @Override
            public void run() {
                // TODO Auto-generated method stub
                crrayList = Soap.GetWebServre("selectAllCargoInfor", arrayList, brrayList);
            }
        }).start();
        

        HashMap<String, String> tempHash = new HashMap<String, String>();
        tempHash.put("Cno", "Cno");
        tempHash.put("Cname", "Cname");
        tempHash.put("Cnum", "Cnum");
        list.add(tempHash);
        
        for (int j = 0; j < crrayList.size(); j += 3) {
            HashMap<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("Cno", crrayList.get(j));
            hashMap.put("Cname", crrayList.get(j + 1));
            hashMap.put("Cnum", crrayList.get(j + 2));
            list.add(hashMap);
        }

        return list;
    }

连接webservice的那个方法HttpConnSoap应该是没问题的因为数据库的增删都是可以的,就是无法实现这个显示所有信息到LISTVIEW中的这个功能不知道为什么,LOGCAT中也是一片绿没什么问题

LOGCAT中的信息:
05-02 15:51:40.642: I/System.out(3678): <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><selectAllCargoInforResponse xmlns="http://tempuri.org/"><selectAllCargoInforResult><string>1</string><string>rice</string><string>100</string><string>2</string><string>dog</string><string>50</string><string>3</string><string>白痴</string><string>25</string></selectAllCargoInforResult></selectAllCargoInforResponse></soap:Body></soap:Envelope>
05-02 15:51:40.647: I/System.out(3678): <?xml version="1.0" encoding="utf-8"?
05-02 15:51:40.647: I/System.out(3678): soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
05-02 15:51:40.647: I/System.out(3678): soap:Body
05-02 15:51:40.647: I/System.out(3678): selectAllCargoInforResponse xmlns="http://tempuri.org/"
05-02 15:51:40.647: I/System.out(3678): selectAllCargoInforResult
05-02 15:51:40.647: I/System.out(3678): 0
05-02 15:51:40.647: I/System.out(3678): string>1</string
05-02 15:51:40.647: I/System.out(3678): string>rice</string
05-02 15:51:40.647: I/System.out(3678): string>100</string
05-02 15:51:40.647: I/System.out(3678): string>2</string
05-02 15:51:40.652: I/System.out(3678): string>dog</string
05-02 15:51:40.652: I/System.out(3678): string>50</string
05-02 15:51:40.652: I/System.out(3678): string>3</string
05-02 15:51:40.652: I/System.out(3678): string>白痴</string
05-02 15:51:40.652: I/System.out(3678): string>25</string
05-02 15:51:40.652: I/System.out(3678): /selectAllCargoInforResult
05-02 15:51:40.652: I/System.out(3678): 1

 

解决方案1:

分析原因就是在线程还没有执行完时候,getAllInfo早已执行完毕以后,,所以在执行for (int j = 0; j < crrayList.size(); j += 3)时候, crrayList为零行。你取出的LOGCAT是执行请求以后的返回数据,这时候setListView方法早已经走完,所以只有一行数据。截图中的一行数据来自       
       HashMap<String, String> tempHash = new HashMap<String, String>();
        tempHash.put("Cno", "Cno");
        tempHash.put("Cname", "Cname");
        tempHash.put("Cnum", "Cnum");
        list.add(tempHash);
使用Thread应该配合Handler来使用。

我把代码修改一下

    private final static int   REQUEST_SUCCESS = 1;
    private final static int   REQUEST_FALSE = 0;
    
    private void RequestData()
    {
        arrayList.clear();
        brrayList.clear();
        crrayList.clear();
        
        new Thread(new Runnable() {
            
            @Override
            public void run() {
                // TODO Auto-generated method stub
                crrayList = Soap.GetWebServre("selectAllCargoInfor", arrayList, brrayList);
                Message msg = new Message(); 
                if(crrayList.size()>0)
                {
                    msg.what = REQUEST_SUCCESS; 
                }
                else 
                {
                    msg.what = REQUEST_FALSE; 
                }
                // 发送消息
                mHandler.sendMessage(msg); 
            }
        }).start();
    }
    
    public Handler mHandler = new Handler(){ 
   
          // 接收消息 
          @Override 
          public void handleMessage(Message msg) { 
              // TODO Auto-generated method stub  
              super.handleMessage(msg); 
              switch (msg.what)
            {
                case REQUEST_SUCCESS:
                    setListView();
                    break;
                case REQUEST_FALSE:
                    // 做错误处理
                    break;
                default:
                    break;
            }
          } 
           
      }; 
       
      private void setListView() {
          listView.setVisibility(View.VISIBLE);
          List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
          list = dbUtil.getAllInfo();
          adapter = new SimpleAdapter(MainActivity.this, list, R.layout.adapter_item, 
          new String[] { "Cno", "Cname", "Cnum" }, 
          new int[] { R.id.txt_Cno, R.id.txt_Cname, R.id.txt_Cnum });
          listView.setAdapter(adapter);
      }
      
      public List<HashMap<String, String>> getAllInfo() {
          List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
          HashMap<String, String> tempHash = new HashMap<String, String>();
          tempHash.put("Cno", "Cno");
          tempHash.put("Cname", "Cname");
          tempHash.put("Cnum", "Cnum");
          list.add(tempHash);
           
          for (int j = 0; j < crrayList.size(); j += 3) {
              HashMap<String, String> hashMap = new HashMap<String, String>();
              hashMap.put("Cno", crrayList.get(j));
              hashMap.put("Cname", crrayList.get(j + 1));
              hashMap.put("Cnum", crrayList.get(j + 2));
              list.add(hashMap);
          }
          return list;
      }

执行RequestData就可以,我没法编译,细节自己再调整看一下,应该能解决问题了。
你给的分数太少了,大牛们都不给你解答。如果解决问题就给分哈。

另外,Java多线程的操作可以系统学习一下。工作中使用极为频繁。

转载于:https://www.cnblogs.com/lengyanyue39/p/3978268.html

相关文章:

  • 从上万监控到实时示警 浙江绍兴餐饮迎智能“食代”
  • win8.1 virtualbox 安装centos7注意事项
  • Java虚拟机在执行程序时内存划分的区域都有哪些?
  • 常见MQTT服务器搭建与试用
  • glutBitmapCharacter及glBitmap在ATI显卡下无法正常显示的原因初探
  • 俄罗斯“光影魔术”展亮相上海科技馆
  • AsyncTasLoader不进行加载操作的原因及解决方法
  • 一文详解Python字符串条件判断方法
  • 前端 CSS 规范
  • SpringBoot 实战 (三) | 配置文件详解
  • 搜索分词---产生新词的公式
  • ORA-22813 ORA-06512
  • MySQL 服务启动y异常: 本地无法启动MySQL服务,报的错误:1067,进程意外终止---解决...
  • Swift—UITextField的基本用法
  • xkb 第3章 虚拟 Modifiers
  • 【译】理解JavaScript:new 关键字
  • ES学习笔记(12)--Symbol
  • javascript 哈希表
  • Vue UI框架库开发介绍
  • vue的全局变量和全局拦截请求器
  • vue数据传递--我有特殊的实现技巧
  • 分享一份非常强势的Android面试题
  • 浮动相关
  • 机器学习 vs. 深度学习
  • 理清楚Vue的结构
  • 如何优雅地使用 Sublime Text
  • 世界编程语言排行榜2008年06月(ActionScript 挺进20强)
  • ​ArcGIS Pro 如何批量删除字段
  • (2)(2.10) LTM telemetry
  • (31)对象的克隆
  • (6)设计一个TimeMap
  • (pytorch进阶之路)扩散概率模型
  • (办公)springboot配置aop处理请求.
  • (二)hibernate配置管理
  • (二)换源+apt-get基础配置+搜狗拼音
  • (附源码)springboot家庭装修管理系统 毕业设计 613205
  • (十三)Maven插件解析运行机制
  • (四)Tiki-taka算法(TTA)求解无人机三维路径规划研究(MATLAB)
  • (原创)boost.property_tree解析xml的帮助类以及中文解析问题的解决
  • (转)甲方乙方——赵民谈找工作
  • .NET MAUI学习笔记——2.构建第一个程序_初级篇
  • .NET 中各种混淆(Obfuscation)的含义、原理、实际效果和不同级别的差异(使用 SmartAssembly)
  • .NET设计模式(7):创建型模式专题总结(Creational Pattern)
  • .NET使用存储过程实现对数据库的增删改查
  • .NET与java的MVC模式(2):struts2核心工作流程与原理
  • [ 云计算 | AWS ] AI 编程助手新势力 Amazon CodeWhisperer:优势功能及实用技巧
  • [2013][note]通过石墨烯调谐用于开关、传感的动态可重构Fano超——
  • [2023-年度总结]凡是过往,皆为序章
  • [23] GaussianAvatars: Photorealistic Head Avatars with Rigged 3D Gaussians
  • [BUAA软工]第一次博客作业---阅读《构建之法》
  • [C#] 我的log4net使用手册
  • [DAU-FI Net开源 | Dual Attention UNet+特征融合+Sobel和Canny等算子解决语义分割痛点]
  • [DevEpxress]GridControl 显示Gif动画
  • [Gamma]阶段测试报告
  • [HNOI2015]实验比较