<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    qileilove

    blog已經轉移至github,大家請訪問 http://qaseven.github.io/

    關于安卓通過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多線程的操作可以系統學習一下。工作中使用極為頻繁

    posted on 2014-12-11 23:42 順其自然EVO 閱讀(881) 評論(0)  編輯  收藏 所屬分類: android

    <2014年12月>
    30123456
    78910111213
    14151617181920
    21222324252627
    28293031123
    45678910

    導航

    統計

    常用鏈接

    留言簿(55)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲国产精品久久久天堂| 国产亚洲漂亮白嫩美女在线| 野花香高清在线观看视频播放免费| 国产zzjjzzjj视频全免费 | 1000部拍拍拍18勿入免费视频下载| 亚洲男人的天堂www| 成全高清在线观看免费| 久久综合日韩亚洲精品色| 99精品一区二区免费视频| 久久亚洲精品国产精品| 国产福利视精品永久免费| 伊人久久亚洲综合影院| 亚洲国产高清视频| **俄罗斯毛片免费| 67194在线午夜亚洲| 宅男666在线永久免费观看| 久久精品国产精品亚洲色婷婷 | 亚洲成在人线中文字幕| 日韩免费一区二区三区在线 | 皇色在线视频免费网站| 亚洲砖码砖专无区2023| 先锋影音资源片午夜在线观看视频免费播放| 日韩亚洲一区二区三区| 3344免费播放观看视频| 亚洲色大成WWW亚洲女子| 亚洲裸男gv网站| 久久国产乱子伦免费精品| 亚洲卡一卡二卡乱码新区| 免费一看一级毛片| 性xxxx视频免费播放直播| 在线a亚洲老鸭窝天堂av高清| 免费人成激情视频| 91精品国产免费久久国语麻豆| 亚洲人成77777在线观看网| 亚洲精品在线视频| 久久www免费人成看片| 丰满妇女做a级毛片免费观看| 免费不卡中文字幕在线| 午夜无码A级毛片免费视频| 亚洲精华国产精华精华液网站| 国产午夜亚洲不卡|