Android连接SQLServer详细教程(数据库+服务器+客户端)

本文为原创,如果转载请注明出处 http://blog.csdn.net/zhyl8157121/article/details/8169172

其实之前发过一篇这样的博文http://blog.csdn.net/zhyl8157121/article/details/7709552,但那个只是简单记录了一些自己的想法,并没有想作为教程来看,后来由于一些朋友想要源代码,就附上了下载地址,但并没有做什么讲解,从现在开始,准备做一份详细的Android如何连接Sqlserver的教程.由于本人才疏学浅,如果有什么不对的地方欢迎大家批评指正.

        为了避免再次被说标题党,这里先说明些事情:

        第一,android没法直接连接SQLServer,起码我没有发现方法,想想看,sqlserver安装之后有多大,android程序是跑在手机上的,想让程序直接访问sqlserver,那手机要多大的内存?

        第二,本文是通过一个“桥梁”——webservice来间接访问SQLServer的,当然还有其他方法,感兴趣的同学可以自行百度。

        如果理解了上面两点,好了咱们继续。

 

         教程会拿一个具体的例子来讲,一步一步来,也许细节上还可以继续加工,但大致的流程就是这样的。

         本教程有五个部分:

  • 项目说明
  • 开发环境部署
  • 数据库设计
  • 服务器端程序设计
  • 客户端(android端)程序设计

项目说明

         这个项目意在实现一个简单的android连接Sqlserver的功能。

         就做一个简单的库存管理功能,包括对仓库内现有货物的查看、货物信息的增加&删除。

开发环境的部署

         今天主要讲解第一个部分,开发环境的部署.

操作系统:Windows764bit 旗舰版

         当然这个是什么基本无所谓,只是我是在这上面开发的,不过家庭普通版的貌似不能配置IIS,就是咱们后面要使用的一个服务.

android端:eclipse + ADT集成开发环境

         相信看到这个教程的基本都知道如何做这些了.如果真的是有哪位同学android开发环境没有配置好而来看这篇教程,请先移步->www.google.com

服务器端:VisualStudio 2010 旗舰版

         这个是用来写website/webservice的,开发语言使用C# (即.net)

数据库:SQLServer2008 R2

         其实这个是什么版本也无所谓吧,教程使用的都是比较基本的东西,所以版本的差异基本可以忽略。

IIS 7.5:正确配置并开启IIS服务

         如果想将website/webservice发布出去就要开启这个服务。但是如果仅仅是在本地进行测试就不需要配置,直接在VS中运行就可以。

         其实我在开发的时候也只是配置IIS的时候遇到了一些问题,这里给出IIS的配置方法.

         http://wenku.baidu.com/view/95cf9fd9ad51f01dc281f1af.html这篇文库给的还是很详细的,我当初就是照着这个配置的。

数据库设计

数据库名称:StockManage

表设计

表名称:C

表说明:

列名

中文名称

数据型态

必填

说明

Cno

货物编号

Int

V

主键,自增

Cname

货物名称

String

 

 

Cnum

货物数量

Int

 

 

下图是设计表的时候的截图。

 

向表中输入内容

 

吐槽一下:为什么这里猫、狗、电话都有,甚至还有Surface?!这只能说当时LZ在想这些……

 

 

服务器端程序设计(Webservice)

         其实服务端可以写成webservice也可以写成website,前者只是提供一种服务,而后者是可以提供用户界面等具体的页面,后者也就是咱们平时所说的“网站”。

         两者的区别:

  • Web Service 只提供程序和接口,不提供用户界面
  • Web Site 提供程序和接口,也提供用户界面(网页)

         由于咱们只是需要一个中介来访问sqlserver,所以写成webservice足够了。

         目标:写一个Website访问Sqlserver,获取数据并转换成xml格式,然后传递给android客户端。

1.      新建一个Webservice工程

2.      视图 -> 其它窗口 -> 服务器资源管理器

3.      右键数据连接 -> 添加连接

4.      选择Microsoft Sqlserver

5.      如下图所示选择(可以点击测试连接来检测连接是否成功,然后点击确定)

6.      数据库的查看和编辑也可以在VS中进行了

7.      先查看一下数据库属性并记录下连接属性

8.      新建一个类DBOperation,代码如下:

[csharp] view plaincopyprint?
 
  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.HtmlControls;  
  9. using System.Web.UI.WebControls;  
  10. using System.Web.UI.WebControls.WebParts;  
  11. using System.Xml.Linq;  
  12. using System.Data.SqlClient;  
  13. using System.Text.RegularExpressions;  
  14. using System.Collections;  
  15. using System.Collections.Generic;  
  16.   
  17. namespace StockManageWebservice  
  18. {  
  19.     /// <summary>   
  20.     /// 一个操作数据库的类,所有对SQLServer的操作都写在这个类中,使用的时候实例化一个然后直接调用就可以   
  21.     /// </summary>   
  22.     public class DBOperation:IDisposable  
  23.     {  
  24.         public static SqlConnection sqlCon;  //用于连接数据库   
  25.   
  26.         //将下面的引号之间的内容换成上面记录下的属性中的连接字符串   
  27.         private String ConServerStr = @"Data Source=BOTTLE-PC;Initial Catalog=StockManage;Integrated Security=True";  
  28.           
  29.         //默认构造函数   
  30.         public DBOperation()  
  31.         {  
  32.             if (sqlCon == null)  
  33.             {  
  34.                 sqlCon = new SqlConnection();  
  35.                 sqlCon.ConnectionString = ConServerStr;  
  36.                 sqlCon.Open();  
  37.             }  
  38.         }  
  39.            
  40.         //关闭/销毁函数,相当于Close()   
  41.         public void Dispose()  
  42.         {  
  43.             if (sqlCon != null)  
  44.             {  
  45.                 sqlCon.Close();  
  46.                 sqlCon = null;  
  47.             }  
  48.         }  
  49.           
  50.         /// <summary>   
  51.         /// 获取所有货物的信息   
  52.         /// </summary>   
  53.         /// <returns>所有货物信息</returns>   
  54.         public List<string> selectAllCargoInfor()  
  55.         {  
  56.             List<string> list = new List<string>();  
  57.   
  58.             try  
  59.             {  
  60.                 string sql = "select * from C";  
  61.                 SqlCommand cmd = new SqlCommand(sql,sqlCon);  
  62.                 SqlDataReader reader = cmd.ExecuteReader();  
  63.   
  64.                 while (reader.Read())  
  65.                 {  
  66.                     //将结果集信息添加到返回向量中   
  67.                     list.Add(reader[0].ToString());  
  68.                     list.Add(reader[1].ToString());  
  69.                     list.Add(reader[2].ToString());  
  70.   
  71.                 }  
  72.   
  73.                 reader.Close();  
  74.                 cmd.Dispose();  
  75.   
  76.             }  
  77.             catch(Exception)  
  78.             {  
  79.   
  80.             }  
  81.             return list;  
  82.         }  
  83.   
  84.         /// <summary>   
  85.         /// 增加一条货物信息   
  86.         /// </summary>   
  87.         /// <param name="Cname">货物名称</param>   
  88.         /// <param name="Cnum">货物数量</param>   
  89.         public bool insertCargoInfo(string Cname, int Cnum)  
  90.         {  
  91.             try  
  92.             {  
  93.                 string sql = "insert into C (Cname,Cnum) values ('" + Cname + "'," + Cnum + ")";  
  94.                 SqlCommand cmd = new SqlCommand(sql, sqlCon);  
  95.                 cmd.ExecuteNonQuery();  
  96.                 cmd.Dispose();  
  97.   
  98.                 return true;  
  99.             }  
  100.             catch (Exception)  
  101.             {  
  102.                 return false;  
  103.             }  
  104.         }  
  105.   
  106.         /// <summary>   
  107.         /// 删除一条货物信息   
  108.         /// </summary>   
  109.         /// <param name="Cno">货物编号</param>   
  110.         public bool deleteCargoInfo(string Cno)  
  111.         {  
  112.             try  
  113.             {  
  114.                 string sql = "delete from C where Cno=" + Cno;  
  115.                 SqlCommand cmd = new SqlCommand(sql, sqlCon);  
  116.                 cmd.ExecuteNonQuery();  
  117.                 cmd.Dispose();  
  118.   
  119.                 return true;  
  120.             }  
  121.             catch (Exception)  
  122.             {  
  123.                 return false;  
  124.             }  
  125.         }  
  126.     }  
  127. }  
[csharp] view plaincopyprint?
 
  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.HtmlControls;  
  9. using System.Web.UI.WebControls;  
  10. using System.Web.UI.WebControls.WebParts;  
  11. using System.Xml.Linq;  
  12. using System.Data.SqlClient;  
  13. using System.Text.RegularExpressions;  
  14. using System.Collections;  
  15. using System.Collections.Generic;  
  16.   
  17. namespace StockManageWebservice  
  18. {  
  19.     /// <summary>  
  20.     /// 一个操作数据库的类,所有对SQLServer的操作都写在这个类中,使用的时候实例化一个然后直接调用就可以  
  21.     /// </summary>  
  22.     public class DBOperation:IDisposable  
  23.     {  
  24.         public static SqlConnection sqlCon;  //用于连接数据库  
  25.   
  26.         //将下面的引号之间的内容换成上面记录下的属性中的连接字符串  
  27.         private String ConServerStr = @"Data Source=BOTTLE-PC;Initial Catalog=StockManage;Integrated Security=True";  
  28.           
  29.         //默认构造函数  
  30.         public DBOperation()  
  31.         {  
  32.             if (sqlCon == null)  
  33.             {  
  34.                 sqlCon = new SqlConnection();  
  35.                 sqlCon.ConnectionString = ConServerStr;  
  36.                 sqlCon.Open();  
  37.             }  
  38.         }  
  39.            
  40.         //关闭/销毁函数,相当于Close()  
  41.         public void Dispose()  
  42.         {  
  43.             if (sqlCon != null)  
  44.             {  
  45.                 sqlCon.Close();  
  46.                 sqlCon = null;  
  47.             }  
  48.         }  
  49.           
  50.         /// <summary>  
  51.         /// 获取所有货物的信息  
  52.         /// </summary>  
  53.         /// <returns>所有货物信息</returns>  
  54.         public List<string> selectAllCargoInfor()  
  55.         {  
  56.             List<string> list = new List<string>();  
  57.   
  58.             try  
  59.             {  
  60.                 string sql = "select * from C";  
  61.                 SqlCommand cmd = new SqlCommand(sql,sqlCon);  
  62.                 SqlDataReader reader = cmd.ExecuteReader();  
  63.   
  64.                 while (reader.Read())  
  65.                 {  
  66.                     //将结果集信息添加到返回向量中  
  67.                     list.Add(reader[0].ToString());  
  68.                     list.Add(reader[1].ToString());  
  69.                     list.Add(reader[2].ToString());  
  70.   
  71.                 }  
  72.   
  73.                 reader.Close();  
  74.                 cmd.Dispose();  
  75.   
  76.             }  
  77.             catch(Exception)  
  78.             {  
  79.   
  80.             }  
  81.             return list;  
  82.         }  
  83.   
  84.         /// <summary>  
  85.         /// 增加一条货物信息  
  86.         /// </summary>  
  87.         /// <param name="Cname">货物名称</param>  
  88.         /// <param name="Cnum">货物数量</param>  
  89.         public bool insertCargoInfo(string Cname, int Cnum)  
  90.         {  
  91.             try  
  92.             {  
  93.                 string sql = "insert into C (Cname,Cnum) values ('" + Cname + "'," + Cnum + ")";  
  94.                 SqlCommand cmd = new SqlCommand(sql, sqlCon);  
  95.                 cmd.ExecuteNonQuery();  
  96.                 cmd.Dispose();  
  97.   
  98.                 return true;  
  99.             }  
  100.             catch (Exception)  
  101.             {  
  102.                 return false;  
  103.             }  
  104.         }  
  105.   
  106.         /// <summary>  
  107.         /// 删除一条货物信息  
  108.         /// </summary>  
  109.         /// <param name="Cno">货物编号</param>  
  110.         public bool deleteCargoInfo(string Cno)  
  111.         {  
  112.             try  
  113.             {  
  114.                 string sql = "delete from C where Cno=" + Cno;  
  115.                 SqlCommand cmd = new SqlCommand(sql, sqlCon);  
  116.                 cmd.ExecuteNonQuery();  
  117.                 cmd.Dispose();  
  118.   
  119.                 return true;  
  120.             }  
  121.             catch (Exception)  
  122.             {  
  123.                 return false;  
  124.             }  
  125.         }  
  126.     }  
  127. }  


9.      修改Service1.asmx.cs代码如下:

[csharp] view plaincopyprint?
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Services;  
  6.   
  7. namespace StockManageWebservice  
  8. {  
  9.     /// <summary>   
  10.     /// Service1 的摘要说明   
  11.     /// </summary>   
  12.     [WebService(Namespace = "http://tempuri.org/")]  
  13.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  14.     [System.ComponentModel.ToolboxItem(false)]  
  15.     // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。   
  16.     // [System.Web.Script.Services.ScriptService]   
  17.     public class Service1 : System.Web.Services.WebService  
  18.     {  
  19.         DBOperation dbOperation = new DBOperation();  
  20.   
  21.         [WebMethod]  
  22.         public string HelloWorld()  
  23.         {  
  24.             return "Hello World";  
  25.         }  
  26.   
  27.         [WebMethod(Description = "获取所有货物的信息")]  
  28.         public string[] selectAllCargoInfor()  
  29.         {  
  30.             return dbOperation.selectAllCargoInfor().ToArray();  
  31.         }  
  32.   
  33.         [WebMethod(Description = "增加一条货物信息")]  
  34.         public bool insertCargoInfo(string Cname, int Cnum)  
  35.         {  
  36.             return dbOperation.insertCargoInfo(Cname, Cnum);  
  37.         }  
  38.   
  39.         [WebMethod(Description = "删除一条货物信息")]  
  40.         public bool deleteCargoInfo(string Cno)  
  41.         {  
  42.             return dbOperation.deleteCargoInfo(Cno);  
  43.         }  
  44.     }  
  45. }  
[csharp] view plaincopyprint?
 
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.Web.Services;  
    6.   
    7. namespace StockManageWebservice  
    8. {  
    9.     /// <summary>  
    10.     /// Service1 的摘要说明  
    11.     /// </summary>  
    12.     [WebService(Namespace = "http://tempuri.org/")]  
    13.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
    14.     [System.ComponentModel.ToolboxItem(false)]  
    15.     // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。  
    16.     // [System.Web.Script.Services.ScriptService]  
    17.     public class Service1 : System.Web.Services.WebService  
    18.     {  
    19.         DBOperation dbOperation = new DBOperation();  
    20.   
    21.         [WebMethod]  
    22.         public string HelloWorld()  
    23.         {  
    24.             return "Hello World";  
    25.         }  
    26.   
    27.         [WebMethod(Description = "获取所有货物的信息")]  
    28.         public string[] selectAllCargoInfor()  
    29.         {  
    30.             return dbOperation.selectAllCargoInfor().ToArray();  
    31.         }  
    32.   
    33.         [WebMethod(Description = "增加一条货物信息")]  
    34.         public bool insertCargoInfo(string Cname, int Cnum)  
    35.         {  
    36.             return dbOperation.insertCargoInfo(Cname, Cnum);  
    37.         }  
    38.   
    39.         [WebMethod(Description = "删除一条货物信息")]  
    40.         public bool deleteCargoInfo(string Cno)  
    41.         {  
    42.             return dbOperation.deleteCargoInfo(Cno);  
    43.         }  
    44.     }  
    45. 10.      运行程序(F5),会自动打开一个浏览器,可以看到如下画面:

      11.  选择相应的功能并传递参数可以实现调试从浏览器中调试程序:

      下图选择的是增加一条货物信息

      12.  程序执行的结果:

      13.另,记住这里的端口名,后面android的程序中添入的端口号就是这个:

      客户端(android端)程序设计

      程序代码:

      1.MainActivity

      1. package com.bottle.stockmanage;  
      2.   
      3. import java.util.ArrayList;  
      4. import java.util.HashMap;  
      5. import java.util.List;  
      6.   
      7. import android.app.Activity;  
      8. import android.app.Dialog;  
      9. import android.os.Bundle;  
      10. import android.view.Gravity;  
      11. import android.view.View;  
      12. import android.view.View.OnClickListener;  
      13. import android.view.Window;  
      14. import android.view.WindowManager;  
      15. import android.widget.Button;  
      16. import android.widget.EditText;  
      17. import android.widget.ListView;  
      18. import android.widget.SimpleAdapter;  
      19. import android.widget.Toast;  
      20.   
      21. public class MainActivity extends Activity{  
      22.   
      23.     private Button btn1;  
      24.     private Button btn2;  
      25.     private Button btn3;  
      26.     private ListView listView;  
      27.     private SimpleAdapter adapter;  
      28.     private DBUtil dbUtil;  
      29.   
      30.     @Override  
      31.     public void onCreate(Bundle savedInstanceState) {  
      32.         super.onCreate(savedInstanceState);  
      33.         setContentView(R.layout.activity_main);  
      34.   
      35.         btn1 = (Button) findViewById(R.id.btn_all);  
      36.         btn2 = (Button) findViewById(R.id.btn_add);  
      37.         btn3 = (Button) findViewById(R.id.btn_delete);  
      38.         listView = (ListView) findViewById(R.id.listView);  
      39.         dbUtil = new DBUtil();  
      40.           
      41.         btn1.setOnClickListener(new OnClickListener() {  
      42.               
      43.             @Override  
      44.             public void onClick(View v) {  
      45.                 hideButton(true);  
      46.                 setListView();  
      47.             }  
      48.         });  
      49.   
      50.         btn2.setOnClickListener(new OnClickListener() {  
      51.               
      52.             @Override  
      53.             public void onClick(View v) {  
      54.                 hideButton(true);  
      55.                 setAddDialog();  
      56.             }  
      57.         });  
      58.   
      59.         btn3.setOnClickListener(new OnClickListener() {  
      60.               
      61.             @Override  
      62.             public void onClick(View v) {  
      63.                 hideButton(true);  
      64.                 setDeleteDialog();  
      65.             }  
      66.         });  
      67.     }  
      68.   
      69.     /** 
      70.      * 设置弹出删除对话框 
      71.      */  
      72.     private void setDeleteDialog() {  
      73.           
      74.         final Dialog dialog = new Dialog(MainActivity.this);  
      75.         dialog.setContentView(R.layout.dialog_delete);  
      76.         dialog.setTitle("输入想要删除的货物的编号");  
      77.         Window dialogWindow = dialog.getWindow();  
      78.         WindowManager.LayoutParams lp = dialogWindow.getAttributes();  
      79.         dialogWindow.setGravity(Gravity.CENTER);  
      80.         dialogWindow.setAttributes(lp);  
      81.   
      82.         final EditText cNoEditText = (EditText) dialog.findViewById(R.id.editText1);  
      83.         Button btnConfirm = (Button) dialog.findViewById(R.id.button1);  
      84.         Button btnCancel = (Button) dialog.findViewById(R.id.button2);  
      85.   
      86.         btnConfirm.setOnClickListener(new OnClickListener() {  
      87.   
      88.             @Override  
      89.             public void onClick(View v) {  
      90.                 dbUtil.deleteCargoInfo(cNoEditText.getText().toString());  
      91.                 dialog.dismiss();  
      92.                 hideButton(false);  
      93.                 Toast.makeText(MainActivity.this, "成功删除数据", Toast.LENGTH_SHORT).show();  
      94.             }  
      95.         });  
      96.   
      97.         btnCancel.setOnClickListener(new OnClickListener() {  
      98.   
      99.             @Override  
      100.             public void onClick(View v) {  
      101.                 dialog.dismiss();  
      102.                 hideButton(false);  
      103.             }  
      104.         });  
      105.           
      106.         dialog.show();  
      107.     }  
      108.   
      109.     /** 
      110.      * 设置弹出添加对话框 
      111.      */  
      112.     private void setAddDialog() {  
      113.   
      114.         final Dialog dialog = new Dialog(MainActivity.this);  
      115.         dialog.setContentView(R.layout.dialog_add);  
      116.         dialog.setTitle("输入添加的货物的信息");  
      117.         Window dialogWindow = dialog.getWindow();  
      118.         WindowManager.LayoutParams lp = dialogWindow.getAttributes();  
      119.         dialogWindow.setGravity(Gravity.CENTER);  
      120.         dialogWindow.setAttributes(lp);  
      121.   
      122.         final EditText cNameEditText = (EditText) dialog.findViewById(R.id.editText1);  
      123.         final EditText cNumEditText = (EditText) dialog.findViewById(R.id.editText2);  
      124.         Button btnConfirm = (Button) dialog.findViewById(R.id.button1);  
      125.         Button btnCancel = (Button) dialog.findViewById(R.id.button2);  
      126.   
      127.         btnConfirm.setOnClickListener(new OnClickListener() {  
      128.   
      129.             @Override  
      130.             public void onClick(View v) {  
      131.                   
      132.                 dbUtil.insertCargoInfo(cNameEditText.getText().toString(), cNumEditText.getText().toString());  
      133.                 dialog.dismiss();  
      134.                 hideButton(false);  
      135.                 Toast.makeText(MainActivity.this, "成功添加数据", Toast.LENGTH_SHORT).show();  
      136.             }  
      137.         });  
      138.   
      139.         btnCancel.setOnClickListener(new OnClickListener() {  
      140.   
      141.             @Override  
      142.             public void onClick(View v) {  
      143.                 dialog.dismiss();  
      144.                 hideButton(false);  
      145.             }  
      146.         });  
      147.         dialog.show();  
      148.     }  
      149.   
      150.     /** 
      151.      * 设置listView 
      152.      */  
      153.     private void setListView() {  
      154.   
      155.         listView.setVisibility(View.VISIBLE);  
      156.   
      157.         List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();  
      158.   
      159.         list = dbUtil.getAllInfo();  
      160.   
      161.         adapter = new SimpleAdapter(  
      162.                 MainActivity.this,   
      163.                 list,   
      164.                 R.layout.adapter_item,   
      165.                 new String[] { "Cno", "Cname", "Cnum" },   
      166.                 new int[] { R.id.txt_Cno, R.id.txt_Cname, R.id.txt_Cnum });  
      167.   
      168.         listView.setAdapter(adapter);  
      169.   
      170.     }  
      171.   
      172.     /** 
      173.      * 设置button的可见性 
      174.      */  
      175.     private void hideButton(boolean result) {  
      176.         if (result) {  
      177.             btn1.setVisibility(View.GONE);  
      178.             btn2.setVisibility(View.GONE);  
      179.             btn3.setVisibility(View.GONE);  
      180.         } else {  
      181.             btn1.setVisibility(View.VISIBLE);  
      182.             btn2.setVisibility(View.VISIBLE);  
      183.             btn3.setVisibility(View.VISIBLE);  
      184.         }  
      185.   
      186.     }  
      187.   
      188.     /** 
      189.      * 返回按钮的重写 
      190.      */  
      191.     @Override  
      192.     public void onBackPressed()  
      193.     {  
      194.         if (listView.getVisibility() == View.VISIBLE) {  
      195.             listView.setVisibility(View.GONE);  
      196.             hideButton(false);  
      197.         }else {  
      198.             MainActivity.this.finish();  
      199.         }  
      200.     }  
      201. }  
      1. package com.bottle.stockmanage;  
      2.   
      3. import java.util.ArrayList;  
      4. import java.util.HashMap;  
      5. import java.util.List;  
      6.   
      7. import android.app.Activity;  
      8. import android.app.Dialog;  
      9. import android.os.Bundle;  
      10. import android.view.Gravity;  
      11. import android.view.View;  
      12. import android.view.View.OnClickListener;  
      13. import android.view.Window;  
      14. import android.view.WindowManager;  
      15. import android.widget.Button;  
      16. import android.widget.EditText;  
      17. import android.widget.ListView;  
      18. import android.widget.SimpleAdapter;  
      19. import android.widget.Toast;  
      20.   
      21. public class MainActivity extends Activity{  
      22.   
      23.     private Button btn1;  
      24.     private Button btn2;  
      25.     private Button btn3;  
      26.     private ListView listView;  
      27.     private SimpleAdapter adapter;  
      28.     private DBUtil dbUtil;  
      29.   
      30.     @Override  
      31.     public void onCreate(Bundle savedInstanceState) {  
      32.         super.onCreate(savedInstanceState);  
      33.         setContentView(R.layout.activity_main);  
      34.   
      35.         btn1 = (Button) findViewById(R.id.btn_all);  
      36.         btn2 = (Button) findViewById(R.id.btn_add);  
      37.         btn3 = (Button) findViewById(R.id.btn_delete);  
      38.         listView = (ListView) findViewById(R.id.listView);  
      39.         dbUtil = new DBUtil();  
      40.           
      41.         btn1.setOnClickListener(new OnClickListener() {  
      42.               
      43.             @Override  
      44.             public void onClick(View v) {  
      45.                 hideButton(true);  
      46.                 setListView();  
      47.             }  
      48.         });  
      49.   
      50.         btn2.setOnClickListener(new OnClickListener() {  
      51.               
      52.             @Override  
      53.             public void onClick(View v) {  
      54.                 hideButton(true);  
      55.                 setAddDialog();  
      56.             }  
      57.         });  
      58.   
      59.         btn3.setOnClickListener(new OnClickListener() {  
      60.               
      61.             @Override  
      62.             public void onClick(View v) {  
      63.                 hideButton(true);  
      64.                 setDeleteDialog();  
      65.             }  
      66.         });  
      67.     }  
      68.   
      69.     /** 
      70.      * 设置弹出删除对话框 
      71.      */  
      72.     private void setDeleteDialog() {  
      73.           
      74.         final Dialog dialog = new Dialog(MainActivity.this);  
      75.         dialog.setContentView(R.layout.dialog_delete);  
      76.         dialog.setTitle("输入想要删除的货物的编号");  
      77.         Window dialogWindow = dialog.getWindow();  
      78.         WindowManager.LayoutParams lp = dialogWindow.getAttributes();  
      79.         dialogWindow.setGravity(Gravity.CENTER);  
      80.         dialogWindow.setAttributes(lp);  
      81.   
      82.         final EditText cNoEditText = (EditText) dialog.findViewById(R.id.editText1);  
      83.         Button btnConfirm = (Button) dialog.findViewById(R.id.button1);  
      84.         Button btnCancel = (Button) dialog.findViewById(R.id.button2);  
      85.   
      86.         btnConfirm.setOnClickListener(new OnClickListener() {  
      87.   
      88.             @Override  
      89.             public void onClick(View v) {  
      90.                 dbUtil.deleteCargoInfo(cNoEditText.getText().toString());  
      91.                 dialog.dismiss();  
      92.                 hideButton(false);  
      93.                 Toast.makeText(MainActivity.this, "成功删除数据", Toast.LENGTH_SHORT).show();  
      94.             }  
      95.         });  
      96.   
      97.         btnCancel.setOnClickListener(new OnClickListener() {  
      98.   
      99.             @Override  
      100.             public void onClick(View v) {  
      101.                 dialog.dismiss();  
      102.                 hideButton(false);  
      103.             }  
      104.         });  
      105.           
      106.         dialog.show();  
      107.     }  
      108.   
      109.     /** 
      110.      * 设置弹出添加对话框 
      111.      */  
      112.     private void setAddDialog() {  
      113.   
      114.         final Dialog dialog = new Dialog(MainActivity.this);  
      115.         dialog.setContentView(R.layout.dialog_add);  
      116.         dialog.setTitle("输入添加的货物的信息");  
      117.         Window dialogWindow = dialog.getWindow();  
      118.         WindowManager.LayoutParams lp = dialogWindow.getAttributes();  
      119.         dialogWindow.setGravity(Gravity.CENTER);  
      120.         dialogWindow.setAttributes(lp);  
      121.   
      122.         final EditText cNameEditText = (EditText) dialog.findViewById(R.id.editText1);  
      123.         final EditText cNumEditText = (EditText) dialog.findViewById(R.id.editText2);  
      124.         Button btnConfirm = (Button) dialog.findViewById(R.id.button1);  
      125.         Button btnCancel = (Button) dialog.findViewById(R.id.button2);  
      126.   
      127.         btnConfirm.setOnClickListener(new OnClickListener() {  
      128.   
      129.             @Override  
      130.             public void onClick(View v) {  
      131.                   
      132.                 dbUtil.insertCargoInfo(cNameEditText.getText().toString(), cNumEditText.getText().toString());  
      133.                 dialog.dismiss();  
      134.                 hideButton(false);  
      135.                 Toast.makeText(MainActivity.this, "成功添加数据", Toast.LENGTH_SHORT).show();  
      136.             }  
      137.         });  
      138.   
      139.         btnCancel.setOnClickListener(new OnClickListener() {  
      140.   
      141.             @Override  
      142.             public void onClick(View v) {  
      143.                 dialog.dismiss();  
      144.                 hideButton(false);  
      145.             }  
      146.         });  
      147.         dialog.show();  
      148.     }  
      149.   
      150.     /** 
      151.      * 设置listView 
      152.      */  
      153.     private void setListView() {  
      154.   
      155.         listView.setVisibility(View.VISIBLE);  
      156.   
      157.         List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();  
      158.   
      159.         list = dbUtil.getAllInfo();  
      160.   
      161.         adapter = new SimpleAdapter(  
      162.                 MainActivity.this,   
      163.                 list,   
      164.                 R.layout.adapter_item,   
      165.                 new String[] { "Cno", "Cname", "Cnum" },   
      166.                 new int[] { R.id.txt_Cno, R.id.txt_Cname, R.id.txt_Cnum });  
      167.   
      168.         listView.setAdapter(adapter);  
      169.   
      170.     }  
      171.   
      172.     /** 
      173.      * 设置button的可见性 
      174.      */  
      175.     private void hideButton(boolean result) {  
      176.         if (result) {  
      177.             btn1.setVisibility(View.GONE);  
      178.             btn2.setVisibility(View.GONE);  
      179.             btn3.setVisibility(View.GONE);  
      180.         } else {  
      181.             btn1.setVisibility(View.VISIBLE);  
      182.             btn2.setVisibility(View.VISIBLE);  
      183.             btn3.setVisibility(View.VISIBLE);  
      184.         }  
      185.   
      186.     }  
      187.   
      188.     /** 
      189.      * 返回按钮的重写 
      190.      */  
      191.     @Override  
      192.     public void onBackPressed()  
      193.     {  
      194.         if (listView.getVisibility() == View.VISIBLE) {  
      195.             listView.setVisibility(View.GONE);  
      196.             hideButton(false);  
      197.         }else {  
      198.             MainActivity.this.finish();  
      199.         }  
      200.     }  
      201. }  

      2.HttpConnSoap
      1. package com.bottle.stockmanage;  
      2.   
      3. import java.io.IOException;  
      4. import java.io.InputStream;  
      5. import java.io.OutputStream;  
      6. import java.net.HttpURLConnection;  
      7. import java.net.URL;  
      8. import java.util.ArrayList;  
      9.   
      10. public class HttpConnSoap {  
      11.     public ArrayList<String> GetWebServre(String methodName, ArrayList<String> Parameters, ArrayList<String> ParValues) {  
      12.         ArrayList<String> Values = new ArrayList<String>();  
      13.           
      14.         //ServerUrl是指webservice的url   
      15.         //10.0.2.2是让android模拟器访问本地(PC)服务器,不能写成127.0.0.1   
      16.         //11125是指端口号,即挂载到IIS上的时候开启的端口   
      17.         //Service1.asmx是指提供服务的页面   
      18.         String ServerUrl = "http://10.0.2.2:11125/Service1.asmx";  
      19.           
      20.         //String soapAction="http://tempuri.org/LongUserId1";   
      21.         String soapAction = "http://tempuri.org/" + methodName;  
      22.         //String data = "";   
      23.         String soap = "<?xml version="1.0" encoding="utf-8"?>"  
      24.                 + "<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">"  
      25.                 + "<soap:Body />";  
      26.         String tps, vps, ts;  
      27.         String mreakString = "";  
      28.   
      29.         mreakString = "<" + methodName + " xmlns="http://tempuri.org/">";  
      30.         for (int i = 0; i < Parameters.size(); i++) {  
      31.             tps = Parameters.get(i).toString();  
      32.             //设置该方法的参数为.net webService中的参数名称   
      33.             vps = ParValues.get(i).toString();  
      34.             ts = "<" + tps + ">" + vps + "</" + tps + ">";  
      35.             mreakString = mreakString + ts;  
      36.         }  
      37.         mreakString = mreakString + "</" + methodName + ">";  
      38.         /* 
      39.         +"<HelloWorld xmlns="http://tempuri.org/">" 
      40.         +"<x>string11661</x>" 
      41.         +"<SF1>string111</SF1>" 
      42.         + "</HelloWorld>" 
      43.         */  
      44.         String soap2 = "</soap:Envelope>";  
      45.         String requestData = soap + mreakString + soap2;  
      46.         //System.out.println(requestData);   
      47.   
      48.         try {  
      49.             URL url = new URL(ServerUrl);  
      50.             HttpURLConnection con = (HttpURLConnection) url.openConnection();  
      51.             byte[] bytes = requestData.getBytes("utf-8");  
      52.             con.setDoInput(true);  
      53.             con.setDoOutput(true);  
      54.             con.setUseCaches(false);  
      55.             con.setConnectTimeout(6000);// 设置超时时间   
      56.             con.setRequestMethod("POST");  
      57.             con.setRequestProperty("Content-Type", "text/xml;charset=utf-8");  
      58.             con.setRequestProperty("SOAPAction", soapAction);  
      59.             con.setRequestProperty("Content-Length", "" + bytes.length);  
      60.             OutputStream outStream = con.getOutputStream();  
      61.             outStream.write(bytes);  
      62.             outStream.flush();  
      63.             outStream.close();  
      64.             InputStream inStream = con.getInputStream();  
      65.   
      66.             //data=parser(inStream);   
      67.             //System.out.print("11");   
      68.             Values = inputStreamtovaluelist(inStream, methodName);  
      69.             //System.out.println(Values.size());   
      70.             return Values;  
      71.   
      72.         } catch (Exception e) {  
      73.             System.out.print("2221");  
      74.             return null;  
      75.         }  
      76.     }  
      77.   
      78.     public ArrayList<String> inputStreamtovaluelist(InputStream in, String MonthsName) throws IOException {  
      79.         StringBuffer out = new StringBuffer();  
      80.         String s1 = "";  
      81.         byte[] b = new byte[4096];  
      82.         ArrayList<String> Values = new ArrayList<String>();  
      83.         Values.clear();  
      84.   
      85.         for (int n; (n = in.read(b)) != -1;) {  
      86.             s1 = new String(b, 0, n);  
      87.             out.append(s1);  
      88.         }  
      89.   
      90.         System.out.println(out);  
      91.         String[] s13 = s1.split("><");  
      92.         String ifString = MonthsName + "Result";  
      93.         String TS = "";  
      94.         String vs = "";  
      95.   
      96.         Boolean getValueBoolean = false;  
      97.         for (int i = 0; i < s13.length; i++) {  
      98.             TS = s13[i];  
      99.             System.out.println(TS);  
      100.             int j, k, l;  
      101.             j = TS.indexOf(ifString);  
      102.             k = TS.lastIndexOf(ifString);  
      103.   
      104.             if (j >= 0) {  
      105.                 System.out.println(j);  
      106.                 if (getValueBoolean == false) {  
      107.                     getValueBoolean = true;  
      108.                 } else {  
      109.   
      110.                 }  
      111.   
      112.                 if ((j >= 0) && (k > j)) {  
      113.                     System.out.println("FFF" + TS.lastIndexOf("/" + ifString));  
      114.                     //System.out.println(TS);   
      115.                     l = ifString.length() + 1;  
      116.                     vs = TS.substring(j + l, k - 2);  
      117.                     //System.out.println("fff"+vs);   
      118.                     Values.add(vs);  
      119.                     System.out.println("退出" + vs);  
      120.                     getValueBoolean = false;  
      121.                     return Values;  
      122.                 }  
      123.   
      124.             }  
      125.             if (TS.lastIndexOf("/" + ifString) >= 0) {  
      126.                 getValueBoolean = false;  
      127.                 return Values;  
      128.             }  
      129.             if ((getValueBoolean) && (TS.lastIndexOf("/" + ifString) < 0) && (j < 0)) {  
      130.                 k = TS.length();  
      131.                 //System.out.println(TS);   
      132.                 vs = TS.substring(7, k - 8);  
      133.                 //System.out.println("f"+vs);   
      134.                 Values.add(vs);  
      135.             }  
      136.   
      137.         }  
      138.   
      139.         return Values;  
      140.     }  
      141.   
      142. }  
      1. package com.bottle.stockmanage;  
      2.   
      3. import java.io.IOException;  
      4. import java.io.InputStream;  
      5. import java.io.OutputStream;  
      6. import java.net.HttpURLConnection;  
      7. import java.net.URL;  
      8. import java.util.ArrayList;  
      9.   
      10. public class HttpConnSoap {  
      11.     public ArrayList<String> GetWebServre(String methodName, ArrayList<String> Parameters, ArrayList<String> ParValues) {  
      12.         ArrayList<String> Values = new ArrayList<String>();  
      13.           
      14.         //ServerUrl是指webservice的url  
      15.         //10.0.2.2是让android模拟器访问本地(PC)服务器,不能写成127.0.0.1  
      16.         //11125是指端口号,即挂载到IIS上的时候开启的端口  
      17.         //Service1.asmx是指提供服务的页面  
      18.         String ServerUrl = "http://10.0.2.2:11125/Service1.asmx";  
      19.           
      20.         //String soapAction="http://tempuri.org/LongUserId1";  
      21.         String soapAction = "http://tempuri.org/" + methodName;  
      22.         //String data = "";  
      23.         String soap = "<?xml version="1.0" encoding="utf-8"?>"  
      24.                 + "<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">"  
      25.                 + "<soap:Body />";  
      26.         String tps, vps, ts;  
      27.         String mreakString = "";  
      28.   
      29.         mreakString = "<" + methodName + " xmlns="http://tempuri.org/">";  
      30.         for (int i = 0; i < Parameters.size(); i++) {  
      31.             tps = Parameters.get(i).toString();  
      32.             //设置该方法的参数为.net webService中的参数名称  
      33.             vps = ParValues.get(i).toString();  
      34.             ts = "<" + tps + ">" + vps + "</" + tps + ">";  
      35.             mreakString = mreakString + ts;  
      36.         }  
      37.         mreakString = mreakString + "</" + methodName + ">";  
      38.         /* 
      39.         +"<HelloWorld xmlns="http://tempuri.org/">" 
      40.         +"<x>string11661</x>" 
      41.         +"<SF1>string111</SF1>" 
      42.         + "</HelloWorld>" 
      43.         */  
      44.         String soap2 = "</soap:Envelope>";  
      45.         String requestData = soap + mreakString + soap2;  
      46.         //System.out.println(requestData);  
      47.   
      48.         try {  
      49.             URL url = new URL(ServerUrl);  
      50.             HttpURLConnection con = (HttpURLConnection) url.openConnection();  
      51.             byte[] bytes = requestData.getBytes("utf-8");  
      52.             con.setDoInput(true);  
      53.             con.setDoOutput(true);  
      54.             con.setUseCaches(false);  
      55.             con.setConnectTimeout(6000);// 设置超时时间  
      56.             con.setRequestMethod("POST");  
      57.             con.setRequestProperty("Content-Type", "text/xml;charset=utf-8");  
      58.             con.setRequestProperty("SOAPAction", soapAction);  
      59.             con.setRequestProperty("Content-Length", "" + bytes.length);  
      60.             OutputStream outStream = con.getOutputStream();  
      61.             outStream.write(bytes);  
      62.             outStream.flush();  
      63.             outStream.close();  
      64.             InputStream inStream = con.getInputStream();  
      65.   
      66.             //data=parser(inStream);  
      67.             //System.out.print("11");  
      68.             Values = inputStreamtovaluelist(inStream, methodName);  
      69.             //System.out.println(Values.size());  
      70.             return Values;  
      71.   
      72.         } catch (Exception e) {  
      73.             System.out.print("2221");  
      74.             return null;  
      75.         }  
      76.     }  
      77.   
      78.     public ArrayList<String> inputStreamtovaluelist(InputStream in, String MonthsName) throws IOException {  
      79.         StringBuffer out = new StringBuffer();  
      80.         String s1 = "";  
      81.         byte[] b = new byte[4096];  
      82.         ArrayList<String> Values = new ArrayList<String>();  
      83.         Values.clear();  
      84.   
      85.         for (int n; (n = in.read(b)) != -1;) {  
      86.             s1 = new String(b, 0, n);  
      87.             out.append(s1);  
      88.         }  
      89.   
      90.         System.out.println(out);  
      91.         String[] s13 = s1.split("><");  
      92.         String ifString = MonthsName + "Result";  
      93.         String TS = "";  
      94.         String vs = "";  
      95.   
      96.         Boolean getValueBoolean = false;  
      97.         for (int i = 0; i < s13.length; i++) {  
      98.             TS = s13[i];  
      99.             System.out.println(TS);  
      100.             int j, k, l;  
      101.             j = TS.indexOf(ifString);  
      102.             k = TS.lastIndexOf(ifString);  
      103.   
      104.             if (j >= 0) {  
      105.                 System.out.println(j);  
      106.                 if (getValueBoolean == false) {  
      107.                     getValueBoolean = true;  
      108.                 } else {  
      109.   
      110.                 }  
      111.   
      112.                 if ((j >= 0) && (k > j)) {  
      113.                     System.out.println("FFF" + TS.lastIndexOf("/" + ifString));  
      114.                     //System.out.println(TS);  
      115.                     l = ifString.length() + 1;  
      116.                     vs = TS.substring(j + l, k - 2);  
      117.                     //System.out.println("fff"+vs);  
      118.                     Values.add(vs);  
      119.                     System.out.println("退出" + vs);  
      120.                     getValueBoolean = false;  
      121.                     return Values;  
      122.                 }  
      123.   
      124.             }  
      125.             if (TS.lastIndexOf("/" + ifString) >= 0) {  
      126.                 getValueBoolean = false;  
      127.                 return Values;  
      128.             }  
      129.             if ((getValueBoolean) && (TS.lastIndexOf("/" + ifString) < 0) && (j < 0)) {  
      130.                 k = TS.length();  
      131.                 //System.out.println(TS);  
      132.                 vs = TS.substring(7, k - 8);  
      133.                 //System.out.println("f"+vs);  
      134.                 Values.add(vs);  
      135.             }  
      136.   
      137.         }  
      138.   
      139.         return Values;  
      140.     }  
      141.   
      142. }  

      3.DBUtil
      1. package com.bottle.stockmanage;  
      2.   
      3. import java.sql.Connection;  
      4. import java.util.ArrayList;  
      5. import java.util.HashMap;  
      6. import java.util.List;  
      7.   
      8. public class DBUtil {  
      9.     private ArrayList<String> arrayList = new ArrayList<String>();  
      10.     private ArrayList<String> brrayList = new ArrayList<String>();  
      11.     private ArrayList<String> crrayList = new ArrayList<String>();  
      12.     private HttpConnSoap Soap = new HttpConnSoap();  
      13.   
      14.     public static Connection getConnection() {  
      15.         Connection con = null;  
      16.         try {  
      17.             //Class.forName("org.gjt.mm.mysql.Driver");   
      18.             //con=DriverManager.getConnection("jdbc:mysql://192.168.0.106:3306/test?useUnicode=true&characterEncoding=UTF-8","root","initial");                
      19.         } catch (Exception e) {  
      20.             //e.printStackTrace();   
      21.         }  
      22.         return con;  
      23.     }  
      24.   
      25.     /** 
      26.      * 获取所有货物的信息 
      27.      *  
      28.      * @return 
      29.      */  
      30.     public List<HashMap<String, String>> getAllInfo() {  
      31.         List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();  
      32.   
      33.         arrayList.clear();  
      34.         brrayList.clear();  
      35.         crrayList.clear();  
      36.   
      37.         crrayList = Soap.GetWebServre("selectAllCargoInfor", arrayList, brrayList);  
      38.   
      39.         HashMap<String, String> tempHash = new HashMap<String, String>();  
      40.         tempHash.put("Cno", "Cno");  
      41.         tempHash.put("Cname", "Cname");  
      42.         tempHash.put("Cnum", "Cnum");  
      43.         list.add(tempHash);  
      44.           
      45.         for (int j = 0; j < crrayList.size(); j += 3) {  
      46.             HashMap<String, String> hashMap = new HashMap<String, String>();  
      47.             hashMap.put("Cno", crrayList.get(j));  
      48.             hashMap.put("Cname", crrayList.get(j + 1));  
      49.             hashMap.put("Cnum", crrayList.get(j + 2));  
      50.             list.add(hashMap);  
      51.         }  
      52.   
      53.         return list;  
      54.     }  
      55.   
      56.     /** 
      57.      * 增加一条货物信息 
      58.      *  
      59.      * @return 
      60.      */  
      61.     public void insertCargoInfo(String Cname, String Cnum) {  
      62.   
      63.         arrayList.clear();  
      64.         brrayList.clear();  
      65.           
      66.         arrayList.add("Cname");  
      67.         arrayList.add("Cnum");  
      68.         brrayList.add(Cname);  
      69.         brrayList.add(Cnum);  
      70.           
      71.         Soap.GetWebServre("insertCargoInfo", arrayList, brrayList);  
      72.     }  
      73.       
      74.     /** 
      75.      * 删除一条货物信息 
      76.      *  
      77.      * @return 
      78.      */  
      79.     public void deleteCargoInfo(String Cno) {  
      80.   
      81.         arrayList.clear();  
      82.         brrayList.clear();  
      83.           
      84.         arrayList.add("Cno");  
      85.         brrayList.add(Cno);  
      86.           
      87.         Soap.GetWebServre("deleteCargoInfo", arrayList, brrayList);  
      88.     }  
      89. }  
      1. package com.bottle.stockmanage;  
      2.   
      3. import java.sql.Connection;  
      4. import java.util.ArrayList;  
      5. import java.util.HashMap;  
      6. import java.util.List;  
      7.   
      8. public class DBUtil {  
      9.     private ArrayList<String> arrayList = new ArrayList<String>();  
      10.     private ArrayList<String> brrayList = new ArrayList<String>();  
      11.     private ArrayList<String> crrayList = new ArrayList<String>();  
      12.     private HttpConnSoap Soap = new HttpConnSoap();  
      13.   
      14.     public static Connection getConnection() {  
      15.         Connection con = null;  
      16.         try {  
      17.             //Class.forName("org.gjt.mm.mysql.Driver");  
      18.             //con=DriverManager.getConnection("jdbc:mysql://192.168.0.106:3306/test?useUnicode=true&characterEncoding=UTF-8","root","initial");               
      19.         } catch (Exception e) {  
      20.             //e.printStackTrace();  
      21.         }  
      22.         return con;  
      23.     }  
      24.   
      25.     /** 
      26.      * 获取所有货物的信息 
      27.      *  
      28.      * @return 
      29.      */  
      30.     public List<HashMap<String, String>> getAllInfo() {  
      31.         List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();  
      32.   
      33.         arrayList.clear();  
      34.         brrayList.clear();  
      35.         crrayList.clear();  
      36.   
      37.         crrayList = Soap.GetWebServre("selectAllCargoInfor", arrayList, brrayList);  
      38.   
      39.         HashMap<String, String> tempHash = new HashMap<String, String>();  
      40.         tempHash.put("Cno", "Cno");  
      41.         tempHash.put("Cname", "Cname");  
      42.         tempHash.put("Cnum", "Cnum");  
      43.         list.add(tempHash);  
      44.           
      45.         for (int j = 0; j < crrayList.size(); j += 3) {  
      46.             HashMap<String, String> hashMap = new HashMap<String, String>();  
      47.             hashMap.put("Cno", crrayList.get(j));  
      48.             hashMap.put("Cname", crrayList.get(j + 1));  
      49.             hashMap.put("Cnum", crrayList.get(j + 2));  
      50.             list.add(hashMap);  
      51.         }  
      52.   
      53.         return list;  
      54.     }  
      55.   
      56.     /** 
      57.      * 增加一条货物信息 
      58.      *  
      59.      * @return 
      60.      */  
      61.     public void insertCargoInfo(String Cname, String Cnum) {  
      62.   
      63.         arrayList.clear();  
      64.         brrayList.clear();  
      65.           
      66.         arrayList.add("Cname");  
      67.         arrayList.add("Cnum");  
      68.         brrayList.add(Cname);  
      69.         brrayList.add(Cnum);  
      70.           
      71.         Soap.GetWebServre("insertCargoInfo", arrayList, brrayList);  
      72.     }  
      73.       
      74.     /** 
      75.      * 删除一条货物信息 
      76.      *  
      77.      * @return 
      78.      */  
      79.     public void deleteCargoInfo(String Cno) {  
      80.   
      81.         arrayList.clear();  
      82.         brrayList.clear();  
      83.           
      84.         arrayList.add("Cno");  
      85.         brrayList.add(Cno);  
      86.           
      87.         Soap.GetWebServre("deleteCargoInfo", arrayList, brrayList);  
      88.     }  
      89. }  

      4.activity_main.xml
      1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
      2.     xmlns:tools="http://schemas.android.com/tools"  
      3.     android:layout_width="fill_parent"  
      4.     android:layout_height="fill_parent" >  
      5.   
      6.     <ListView  
      7.         android:id="@+id/listView"  
      8.         android:layout_width="fill_parent"  
      9.         android:layout_height="fill_parent"  
      10.         android:visibility="gone" >  
      11.     </ListView>  
      12.   
      13.     <Button  
      14.         android:id="@+id/btn_all"  
      15.         android:layout_width="wrap_content"  
      16.         android:layout_height="wrap_content"  
      17.         android:layout_above="@+id/btn_add"  
      18.         android:layout_alignLeft="@+id/btn_add"  
      19.         android:layout_marginBottom="10dip"  
      20.         android:text="@string/btn1" />  
      21.   
      22.     <Button  
      23.         android:id="@+id/btn_add"  
      24.         android:layout_width="wrap_content"  
      25.         android:layout_height="wrap_content"  
      26.         android:layout_centerHorizontal="true"  
      27.         android:layout_centerVertical="true"  
      28.         android:text="@string/btn2" />  
      29.   
      30.     <Button  
      31.         android:id="@+id/btn_delete"  
      32.         android:layout_width="wrap_content"  
      33.         android:layout_height="wrap_content"  
      34.         android:layout_alignLeft="@+id/btn_add"  
      35.         android:layout_below="@+id/btn_add"  
      36.         android:layout_marginTop="10dip"  
      37.         android:text="@string/btn3" />  
      38.   
      39. </RelativeLayout>  
      1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
      2.     xmlns:tools="http://schemas.android.com/tools"  
      3.     android:layout_width="fill_parent"  
      4.     android:layout_height="fill_parent" >  
      5.   
      6.     <ListView  
      7.         android:id="@+id/listView"  
      8.         android:layout_width="fill_parent"  
      9.         android:layout_height="fill_parent"  
      10.         android:visibility="gone" >  
      11.     </ListView>  
      12.   
      13.     <Button  
      14.         android:id="@+id/btn_all"  
      15.         android:layout_width="wrap_content"  
      16.         android:layout_height="wrap_content"  
      17.         android:layout_above="@+id/btn_add"  
      18.         android:layout_alignLeft="@+id/btn_add"  
      19.         android:layout_marginBottom="10dip"  
      20.         android:text="@string/btn1" />  
      21.   
      22.     <Button  
      23.         android:id="@+id/btn_add"  
      24.         android:layout_width="wrap_content"  
      25.         android:layout_height="wrap_content"  
      26.         android:layout_centerHorizontal="true"  
      27.         android:layout_centerVertical="true"  
      28.         android:text="@string/btn2" />  
      29.   
      30.     <Button  
      31.         android:id="@+id/btn_delete"  
      32.         android:layout_width="wrap_content"  
      33.         android:layout_height="wrap_content"  
      34.         android:layout_alignLeft="@+id/btn_add"  
      35.         android:layout_below="@+id/btn_add"  
      36.         android:layout_marginTop="10dip"  
      37.         android:text="@string/btn3" />  
      38.   
      39. </RelativeLayout>  

      5.adapter_item.xml
      1. <?xml version="1.0" encoding="utf-8"?>  
      2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  
      3.     android:layout_width="fill_parent"  
      4.     android:layout_height="wrap_content"  
      5.     android:descendantFocusability="blocksDescendants"  
      6.     android:gravity="center" >  
      7.   
      8.     <TableRow  
      9.         android:id="@+id/classroom_detail_item_tableRow"  
      10.         android:layout_width="fill_parent"  
      11.         android:layout_height="wrap_content"  
      12.         android:gravity="center" >  
      13.   
      14.         <TextView  
      15.             android:id="@+id/txt_Cno"  
      16.             android:layout_width="80dp"  
      17.             android:layout_height="wrap_content"  
      18.             android:gravity="center"  
      19.             android:height="40dp"  
      20.             android:textSize="14sp" >  
      21.         </TextView>  
      22.   
      23.         <TextView  
      24.             android:id="@+id/txt_Cname"  
      25.             android:layout_width="80dp"  
      26.             android:layout_height="wrap_content"  
      27.             android:gravity="center"  
      28.             android:height="40dp"  
      29.             android:textSize="14sp" >  
      30.         </TextView>  
      31.   
      32.         <TextView  
      33.             android:id="@+id/txt_Cnum"  
      34.             android:layout_width="80dp"  
      35.             android:layout_height="wrap_content"  
      36.             android:gravity="center"  
      37.             android:height="40dp"  
      38.             android:textSize="14sp" >  
      39.         </TextView>  
      40.   
      41.     </TableRow>  
      42.   
      43. </TableLayout>  
      1. <?xml version="1.0" encoding="utf-8"?>  
      2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  
      3.     android:layout_width="fill_parent"  
      4.     android:layout_height="wrap_content"  
      5.     android:descendantFocusability="blocksDescendants"  
      6.     android:gravity="center" >  
      7.   
      8.     <TableRow  
      9.         android:id="@+id/classroom_detail_item_tableRow"  
      10.         android:layout_width="fill_parent"  
      11.         android:layout_height="wrap_content"  
      12.         android:gravity="center" >  
      13.   
      14.         <TextView  
      15.             android:id="@+id/txt_Cno"  
      16.             android:layout_width="80dp"  
      17.             android:layout_height="wrap_content"  
      18.             android:gravity="center"  
      19.             android:height="40dp"  
      20.             android:textSize="14sp" >  
      21.         </TextView>  
      22.   
      23.         <TextView  
      24.             android:id="@+id/txt_Cname"  
      25.             android:layout_width="80dp"  
      26.             android:layout_height="wrap_content"  
      27.             android:gravity="center"  
      28.             android:height="40dp"  
      29.             android:textSize="14sp" >  
      30.         </TextView>  
      31.   
      32.         <TextView  
      33.             android:id="@+id/txt_Cnum"  
      34.             android:layout_width="80dp"  
      35.             android:layout_height="wrap_content"  
      36.             android:gravity="center"  
      37.             android:height="40dp"  
      38.             android:textSize="14sp" >  
      39.         </TextView>  
      40.   
      41.     </TableRow>  
      42.   
      43. </TableLayout>  

      6.dialog_add.xml
      1. <?xml version="1.0" encoding="utf-8"?>  
      2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
      3.     android:layout_width="fill_parent"  
      4.     android:layout_height="fill_parent"  
      5.     android:orientation="vertical" >  
      6.   
      7.     <EditText  
      8.         android:id="@+id/editText1"  
      9.         android:layout_width="fill_parent"  
      10.         android:layout_height="wrap_content"  
      11.         android:ems="10"  
      12.         android:hint="@string/add_hint1" >  
      13.   
      14.         <requestFocus />  
      15.     </EditText>  
      16.   
      17.     <EditText  
      18.         android:id="@+id/editText2"  
      19.         android:layout_width="fill_parent"  
      20.         android:layout_height="wrap_content"  
      21.         android:ems="10"  
      22.         android:hint="@string/add_hint2"  
      23.         android:inputType="number" />  
      24.   
      25.     <LinearLayout  
      26.         android:layout_width="fill_parent"  
      27.         android:layout_height="wrap_content"  
      28.         android:orientation="horizontal" >  
      29.   
      30.         <Button  
      31.             android:id="@+id/button1"  
      32.             android:layout_width="100dip"  
      33.             android:layout_height="wrap_content"  
      34.             android:layout_marginLeft="20dip"  
      35.             android:text="@string/confirm" />  
      36.   
      37.         <Button  
      38.             android:id="@+id/button2"  
      39.             android:layout_width="100dip"  
      40.             android:layout_height="wrap_content"  
      41.             android:layout_marginLeft="40dip"  
      42.             android:text="@string/cancel" />  
      43.     </LinearLayout>  
      44.   
      45. </LinearLayout>  
      1. <?xml version="1.0" encoding="utf-8"?>  
      2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
      3.     android:layout_width="fill_parent"  
      4.     android:layout_height="fill_parent"  
      5.     android:orientation="vertical" >  
      6.   
      7.     <EditText  
      8.         android:id="@+id/editText1"  
      9.         android:layout_width="fill_parent"  
      10.         android:layout_height="wrap_content"  
      11.         android:ems="10"  
      12.         android:hint="@string/add_hint1" >  
      13.   
      14.         <requestFocus />  
      15.     </EditText>  
      16.   
      17.     <EditText  
      18.         android:id="@+id/editText2"  
      19.         android:layout_width="fill_parent"  
      20.         android:layout_height="wrap_content"  
      21.         android:ems="10"  
      22.         android:hint="@string/add_hint2"  
      23.         android:inputType="number" />  
      24.   
      25.     <LinearLayout  
      26.         android:layout_width="fill_parent"  
      27.         android:layout_height="wrap_content"  
      28.         android:orientation="horizontal" >  
      29.   
      30.         <Button  
      31.             android:id="@+id/button1"  
      32.             android:layout_width="100dip"  
      33.             android:layout_height="wrap_content"  
      34.             android:layout_marginLeft="20dip"  
      35.             android:text="@string/confirm" />  
      36.   
      37.         <Button  
      38.             android:id="@+id/button2"  
      39.             android:layout_width="100dip"  
      40.             android:layout_height="wrap_content"  
      41.             android:layout_marginLeft="40dip"  
      42.             android:text="@string/cancel" />  
      43.     </LinearLayout>  
      44.   
      45. </LinearLayout>  

      7.dialog_delete.xml
      1. <?xml version="1.0" encoding="utf-8"?>  
      2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
      3.     android:layout_width="fill_parent"  
      4.     android:layout_height="fill_parent"  
      5.     android:orientation="vertical" >  
      6.   
      7.     <EditText  
      8.         android:id="@+id/editText1"  
      9.         android:layout_width="fill_parent"  
      10.         android:layout_height="wrap_content"  
      11.         android:ems="10"  
      12.         android:hint="@string/delete_hint" >  
      13.   
      14.         <requestFocus />  
      15.     </EditText>  
      16.   
      17.     <LinearLayout  
      18.         android:layout_width="fill_parent"  
      19.         android:layout_height="wrap_content"  
      20.         android:orientation="horizontal" >  
      21.   
      22.         <Button  
      23.             android:id="@+id/button1"  
      24.             android:layout_width="100dip"  
      25.             android:layout_height="wrap_content"  
      26.             android:layout_marginLeft="20dip"  
      27.             android:text="@string/confirm" />  
      28.   
      29.         <Button  
      30.             android:id="@+id/button2"  
      31.             android:layout_width="100dip"  
      32.             android:layout_height="wrap_content"  
      33.             android:layout_marginLeft="40dip"  
      34.             android:text="@string/cancel" />  
      35.     </LinearLayout>  
      36.   
      37. </LinearLayout>  
      1. <?xml version="1.0" encoding="utf-8"?>  
      2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
      3.     android:layout_width="fill_parent"  
      4.     android:layout_height="fill_parent"  
      5.     android:orientation="vertical" >  
      6.   
      7.     <EditText  
      8.         android:id="@+id/editText1"  
      9.         android:layout_width="fill_parent"  
      10.         android:layout_height="wrap_content"  
      11.         android:ems="10"  
      12.         android:hint="@string/delete_hint" >  
      13.   
      14.         <requestFocus />  
      15.     </EditText>  
      16.   
      17.     <LinearLayout  
      18.         android:layout_width="fill_parent"  
      19.         android:layout_height="wrap_content"  
      20.         android:orientation="horizontal" >  
      21.   
      22.         <Button  
      23.             android:id="@+id/button1"  
      24.             android:layout_width="100dip"  
      25.             android:layout_height="wrap_content"  
      26.             android:layout_marginLeft="20dip"  
      27.             android:text="@string/confirm" />  
      28.   
      29.         <Button  
      30.             android:id="@+id/button2"  
      31.             android:layout_width="100dip"  
      32.             android:layout_height="wrap_content"  
      33.             android:layout_marginLeft="40dip"  
      34.             android:text="@string/cancel" />  
      35.     </LinearLayout>  
      36.   
      37. </LinearLayout>  

      8.strings.xml
      1. <resources>  
      2.   
      3.     <string name="app_name">StockManagement</string>  
      4.     <string name="menu_settings">Settings</string>  
      5.     <string name="title_activity_main">MainActivity</string>  
      6.     <string name="btn1">查看所有货物信息</string>  
      7.     <string name="btn2">增加一条货物信息</string>  
      8.     <string name="btn3">删除一条货物信息</string>  
      9.     <string name="add_hint1">输入添加的货物的名称</string>  
      10.     <string name="add_hint2">输入货物的数量</string>  
      11.     <string name="confirm">确定</string>  
      12.     <string name="cancel">取消</string>  
      13.     <string name="delete_hint">输入删除的货物的编号</string>  
      14.   
      15. </resources>  
      1. <resources>  
      2.   
      3.     <string name="app_name">StockManagement</string>  
      4.     <string name="menu_settings">Settings</string>  
      5.     <string name="title_activity_main">MainActivity</string>  
      6.     <string name="btn1">查看所有货物信息</string>  
      7.     <string name="btn2">增加一条货物信息</string>  
      8.     <string name="btn3">删除一条货物信息</string>  
      9.     <string name="add_hint1">输入添加的货物的名称</string>  
      10.     <string name="add_hint2">输入货物的数量</string>  
      11.     <string name="confirm">确定</string>  
      12.     <string name="cancel">取消</string>  
      13.     <string name="delete_hint">输入删除的货物的编号</string>  
      14.   
      15. </resources>  

      9.Manifest.xml
      1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
      2.     package="com.bottle.stockmanage"  
      3.     android:versionCode="1"  
      4.     android:versionName="1.0" >  
      5.   
      6.     <uses-sdk  
      7.         android:minSdkVersion="7"  
      8.         android:targetSdkVersion="15" />  
      9.   
      10.     <uses-permission android:name="android.permission.INTERNET" />  
      11.   
      12.     <application  
      13.         android:icon="@drawable/ic_launcher"  
      14.         android:label="@string/app_name"  
      15.         android:theme="@android:style/Theme.NoTitleBar" >  
      16.         <activity  
      17.             android:name=".MainActivity"  
      18.             android:label="@string/title_activity_main"  
      19.             android:screenOrientation="portrait" >  
      20.             <intent-filter>  
      21.                 <action android:name="android.intent.action.MAIN" />  
      22.   
      23.                 <category android:name="android.intent.category.LAUNCHER" />  
      24.             </intent-filter>  
      25.         </activity>  
      26.     </application>  
      27.   
      28. </manifest>  
      1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
      2.     package="com.bottle.stockmanage"  
      3.     android:versionCode="1"  
      4.     android:versionName="1.0" >  
      5.   
      6.     <uses-sdk  
      7.         android:minSdkVersion="7"  
      8.         android:targetSdkVersion="15" />  
      9.   
      10.     <uses-permission android:name="android.permission.INTERNET" />  
      11.   
      12.     <application  
      13.         android:icon="@drawable/ic_launcher"  
      14.         android:label="@string/app_name"  
      15.         android:theme="@android:style/Theme.NoTitleBar" >  
      16.         <activity  
      17.             android:name=".MainActivity"  
      18.             android:label="@string/title_activity_main"  
      19.             android:screenOrientation="portrait" >  
      20.             <intent-filter>  
      21.                 <action android:name="android.intent.action.MAIN" />  
      22.   
      23.                 <category android:name="android.intent.category.LAUNCHER" />  
      24.             </intent-filter>  
      25.         </activity>  
      26.     </application>  
      27.   
      28. </manifest>  

      运行程序的效果如下图所示:

      再说一下IIS,如果只是在本地进行测试等操作,是不需要使用到IIS的,但是如果想发布出去,就要配置一下IIS。

      好啦,基本就是这样了。程序不是完善的,但大概的思路就是这样,用到的技术也大概就是这几样,但是每一样拿出来都够学一阵的了。

      --->2012.12.02 增加内容

      附上本文demo的CSDN下载地址

      http://download.csdn.net/detail/zhyl8157121/4836107

      --->2013.01.08 增加内容

      解释一下android端如何和webservice通信的。(如何修改实例程序)

      具体的更深层的东西已经在HttpSoap中封装好了,所以大家使用的时候可以直接用这个类就可以了。(我也不懂是怎么实现的……)

      android调用的方法就是如DBUtil中那样,比如内嵌图片 2

      其中arrayList中和brrayList中分别存放对应的webservice中“selectAllCargoInfor”方法的参数名和参数的值。

      内嵌图片 3

      由于webservice中的selectAllCargoInfo方法的参数为空,所以对应的,android端调用的时候,arrayList和brrayList的值就是空的。

       所以大家在使用的时候,只需要将webservice中的方法写好,然后写好DBUtil中的调用参数即可。

      --->2013.03.23 增加内容

      如果获取值为空,可能是返回值是复杂类型造成的,可以参考:http://blog.csdn.net/zhyl8157121/article/details/8709048

      先这样,有什么问题可以给我发邮件 bottle.liang@gmail.com

      谢谢您的支持,欢迎大家批评指正。

脚踏实地,心存高远。
原文地址:https://www.cnblogs.com/wanyangke/p/4361749.html