關於安卓通過webservice訪問資料庫問題

來源:互聯網
上載者:User

標籤:http   io   ar   os   使用   java   sp   for   資料   

============問題描述============


訪問資料庫時,手機能增刪資料庫的資料就是顯示不了資料庫的裡的資料不知道是哪裡的問題,用的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() {@Overridepublic void run() {// TODO Auto-generated method stubcrrayList = 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多線程的操作可以系統學習一下。工作中使用極為頻繁。

關於安卓通過webservice訪問資料庫問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.