加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

AndroidStudio手机通过WebService远程访问SqlServer

(2018-09-21 17:32:58)
标签:

androidstudio

开发手机应用

通过webservice

远程访问sqlserver

分类: 代码天地

一 开发环境

操作系统:Win7 64位
数据库:Sql Server2008 R2
服务器端:VS2010    WebService
配置IIS服务,IE 浏览器
Android Studio 设计手机客户端

在模拟器 和 手机上测试
当然也参考了网上的资料


二 数据库设计

创建数据库BOOK,  表 user,  字段和内容 如下:

id     name     age
1      李芳      18
2      王苗      20
3      王永      23


以下第三、 四步 详细参看 


三 C# 创建 WebService

在 vs2010开发环境下 

1 文件--新建--项目
  模板选择 Visual C# -- Web , 具体选择 ASP.NET 空 Web 应用程序
  名称默认:WebApplication1  确定
2 创建 操作数据库的类
 右击 解决方案资源管理器中的 WebApplication1 -- 添加 -- 新建项
 在画面中选择  类, 名称输入 SQLhelper.cs,按 确定 返回

有关代码如下:

3 DBOperation.cs


using System;
using System.Data;
using System.Con figuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text.R egularExp ressions;
using System.Collections;
using System.Collections.Generic;

namespace WebApplication1
{
    /// < summary> 
    /// 一个操作数据库的类,所有对SQLServer的操作都写在这个类中,使用的时候实例化一个然后直接调用就可以
    /// < /summary> 
    public class DBOperation : IDisposable
    {
        public static SqlConnection sqlCon;  //用于连接数据库

        //下面IP地址适合手机在一个局域网内, 或者是全球唯一地址
        private String ConServerStr = @"Data Source=10.3.1.37;Initial Catalog=book;User ID=sa;password=123;Integrated Security=True";

        //默认构造函数
        public DBOperation()
        {
            if (sqlCon == null)
            {
                sqlCon = new SqlConnection();
                sqlCon.ConnectionString = ConServerStr;
                sqlCon.Open();
            }
        }

        //关闭/销毁函数,相当于Close()
        public void Dispose()
        {
            if (sqlCon != null)
            {
                sqlCon.Close();
                sqlCon = null;
            }
        }

        /// < summary> 
        /// 获取所有用户的信息
        /// < /summary> 
        /// < returns> 所有用户信息< /returns> 
        public List< string>  selectAllUser()
        {
            List< string>  list = new List< string> ();

            try
            {
                string sql = "select * from [user]";
                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 (Ex ception e)
            {

            }
            return list;
        }
        /// < summary> 
        /// 增加用户
        /// < /summary> 
        /// < param name="name"> 名称< /param> 
        /// < param name="age"> 年龄< /param> 
        public bool insertOneUser(string name, int age)
        {
            try
            {
                string sql = "insert into [user] (name,age) values ('" + name + "'," + age + ")";
                SqlCommand cmd = new SqlCommand(sql, sqlCon);
                cmd.ExecuteNonQuery();
                cmd.Dispose();

                return true;
            }
            catch (Ex ception)
            {
                return false;
            }
        }

        /// < summary> 
        /// 删除用户
        /// < /summary> 
        /// < param name="id"> 用户id号< /param> 
        public bool deleteOneUser(string id)
        {
            try
            {
                string sql = "delete from [user] where id=" + id;
                SqlCommand cmd = new SqlCommand(sql, sqlCon);
                cmd.ExecuteNonQuery();
                cmd.Dispose();

                return true;
            }
            catch (Ex ception)
            {
                return false;
            }
        }
    }

}


4  WebService1.asmx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using MyProject;

namespace WebApplication1
{
    /// < summary> 
    /// WebService1 的摘要说明
    /// < /summary> 
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {
        DBOperation dbOperation = new DBOperation();

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod(Description = "1 获得所有用户")]
        public string[] selectAllUser()
        {
            return dbOperation.selectAllUser().ToArray();
        }

        [WebMethod(Description = "2 增加用户")]
        public bool insertOneUser(string name, int age)
        {
            return dbOperation.insertOneUser(name, age);
        }

        [WebMethod(Description = "3 删除用户")]
        public bool deleteOneUser(string id)
        {
            return dbOperation.deleteOneUser(id);
        }
    }
}


四 浏览器测试网站访问

 创建网站

1  IIS设置
 控制面板--程序和功能--打开或关闭Windows功能--Internet信息服务--
 在Web管理工具 和 万维网服务 下,勾选相全部勾选
2 新建网站
  右击桌面上的 “计算机”--管理--服务和应用程序--Internet信息服务(IIS)管理
  在 Internet 信息服务(IIS)管理器中,展开左侧列表,并右击“网站”--“新建网站”
  网站名称随便 myWeb1, 物理路径 D:\MyWebService
  点 点击右上部“选择”,从中选择 应用程序池  比如:Classic.Net AppPool
  端口号:8070 

3 入站规则
 一机布置多个网站,用端口号来区分,建立 入站的规则
 点击 控制面板 右上角 的 查看方式,从 类别 改为 小图标,
 控制面板 -- Windows防火墙 -- 左下侧的 高级设置 -- 左侧的 入站规则 -- 右上侧的 新建规则
  
  TCP 方式,端口号:8070 ,其它都默认

4 浏览器测试
http://localhost:8070/WebService1.asmx/selectAllUser

< ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/"> 
< string> 1< /string> 
< string> 李芳< /string> 
< string> 18< /string> 
< string> 2< /string> 
< string> 王苗< /string> 
< string> 20< /string> 
< string> 3< /string> 
< string> 王永< /string> 
< string> 23< /string> 
< /ArrayOfString> 


五 手机客户端编程设计

 使用 Android Studio 开发

  在Android Studio环境下创建一个项目

  File -- New -- New Project... 
  工程名称 webservice
  
1 主MainActivity.java 

package com.example.webservice;

       import android.support.v7.app.AppCompatActivity;
       import android.os.Bundle;
        import java.util.ArrayList;
        import java.util.HashMap;
        import java.util.List;

        import android.app.Activity;
        import android.app.Dialog;
        import android.os.Bundle;
        import android.view.Gravity;
        import android.view.View;
        import android.view.View.OnClickListener;
       import android.view.ViewGroup;
       import android.view.Window;
        import android.view.WindowManager;
        import android.widget.Button;
        import android.widget.EditText;
       import android.widget.ListAdapter;
       import android.widget.ListView;
        import android.widget.SimpleAdapter;
        import android.widget.Toast;

public class MainActivity extends Activity{

    private Button btn_all;
    private Button btn_add;
    private Button btn_delete;
    private ListView listView;
    private SimpleAdapter adapter;
    private DBUtil dbUtil;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_all = (Button) findViewById(R.id.btn_all);
        btn_add = (Button) findViewById(R.id.btn_add);
        btn_delete = (Button) findViewById(R.id.btn_delete);
        listView = (ListView) findViewById(R.id.listView);
        dbUtil = new DBUtil();

      btn_all.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                hideButton(true);
                    setListView();
            }
        });

        btn_add.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                hideButton(true);
                setAddDialog();
            }
        });

        btn_delete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                hideButton(true);
                setDeleteDialog();
            }
        });
    }

//      删除对话框
     
    private void setDeleteDialog() {

        final Dialog dialog = new Dialog(MainActivity.this);
        dialog.setContentView(R.layout.dialog_delete);
        dialog.setTitle("输入想要删除的用户编号");
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.CENTER);
        dialogWindow.setAttributes(lp);

        final EditText idEditText = (EditText) dialog.findViewById(R.id.idEditText);
        Button btnConfirm = (Button) dialog.findViewById(R.id.but_ok);
        Button btnReturn = (Button) dialog.findViewById(R.id.but_ret);

       btnConfirm.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Runnable run = new Runnable()
                {
                    @Override
                    public void run()
                    {
                dbUtil.deleteUser(cNoEditText.getText().toString());
                dialog.dismiss();
                //hideButton(false);
               // Toast.makeText(MainActivity.this, "成功删除数据", Toast.LENGTH_SHORT).show();
                     }
                };
                new Thread(run).start();
                hideButton(false);
                Toast.makeText(MainActivity.this, "成功删除数据", Toast.LENGTH_SHORT).show();

            }
        });

        btnReturn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
                hideButton(false);
            }
        });

        dialog.show();
    }

//添加对话框
  private void setAddDialog() {

        final Dialog dialog = new Dialog(MainActivity.this);
        dialog.setContentView(R.layout.dialog_add);
        dialog.setTitle("输入添加的用户用户信息");
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.CENTER);
        dialogWindow.setAttributes(lp);

        final EditText cNameEditText = (EditText) dialog.findViewById(R.id.editText1);
        final EditText cNumEditText = (EditText) dialog.findViewById(R.id.editText2);
        Button btnConfirm = (Button) dialog.findViewById(R.id.button1);
        Button btnReturn = (Button) dialog.findViewById(R.id.button2);

        btnConfirm.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Runnable run = new Runnable()
                {
                    @Override
                    public void run()
                    {
                dbUtil.insertUser(cNameEditText.getText().toString(), cNumEditText.getText().toString());
                dialog.dismiss();
                //hideButton(false);
                //Toast.makeText(MainActivity.this, "成功添加数据", Toast.LENGTH_SHORT).show();
                    }
                };
                new Thread(run).start();
                hideButton(false);
                Toast.makeText(MainActivity.this, "成功添加数据", Toast.LENGTH_SHORT).show();

            }
        });

        btnReturn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
                hideButton(false);
            }
        });
        dialog.show();
    }
    
//显示用户
     
    private void setListView() {

        final Dialog dialog = new Dialog(MainActivity.this);
        dialog.setContentView(R.layout.dialog_show);
        dialog.setTitle("显示人员的信息");
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.CENTER);
        dialogWindow.setAttributes(lp);
        final ListView listView= (ListView) dialog.findViewById(R.id.listView);
        final List< HashMap< String, String> >  list = new ArrayList< HashMap< String, String> > ();
        Button btnReturn = (Button) dialog.findViewById(R.id.btn_return);

        Runnable run = new Runnable()
        {
            @Override
            public void run()
            {
            list.addAll( dbUtil.getAllInfo());
            adapter = new SimpleAdapter(
                MainActivity.this,
                list,
                R.layout.adapter_item,
                new String[] { "id", "name", "age" },
                new int[] { R.id.txt_id, R.id.txt_name, R.id.txt_age });

               listView.setAdapter(adapter);
              
            }
        };

        new Thread(run).start();

        btnReturn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
                hideButton(false);
            }
        });

        dialog.show();


                }

//button 可见性
   
    private void hideButton(boolean result) {
        if (result) {
            btn_delete.setVisibility(View.GONE);
            btn_add.setVisibility(View.GONE);
            btn_all.setVisibility(View.GONE);
        } else {
            btn_delete.setVisibility(View.VISIBLE);
            btn_add.setVisibility(View.VISIBLE);
            btn_all.setVisibility(View.VISIBLE);
        }

    }

}

2 连接代理的代码 HttpConnSoap.java

package com.example.webservice;

import java.io.IOEx ception;
        import java.io.InputStream;
        import java.io.OutputStream;
        import java.net.HttpURLConnection;
        import java.net.URL;
        import java.util.ArrayList;

public class HttpConnSoap {
    public ArrayList< String>  GetWebServre(String methodName, ArrayList< String>  Parameters, ArrayList< String>  ParValues) {
        ArrayList< String>  Values = new ArrayList< String> ();

        //ServerUrl是指webservice的url
        //10.3.1.37是让android模拟器访问本地(PC)服务器 
        //8070是指端口号,即挂载到IIS上的时候开启的端口
        //WebService1.asmx是指提供服务的页面
        String ServerUrl = "http://10.3.1.37:8070/WebService1.asmx";
        String soapAction = "http://tempuri.org/" + methodName;
        //String data = "";
        String soap = "< ?xml version=\"1.0\" encoding=\"utf-8\"?> "
                + "< 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/\"> "
                + "< soap:Body /> ";
        String tps, vps, ts;
        String mreakString = "";

        mreakString = "< " + methodName + " xmlns=\"http://tempuri.org/\"> ";
        for (int i = 0; i <  Parameters.size(); i++) {
            tps = Parameters.get(i).toString();
            //设置该方法的参数为.net webService中的参数名称
            vps = ParValues.get(i).toString();
            ts = "< " + tps + "> " + vps + "< /" + tps + "> ";
            mreakString = mreakString + ts;
        }
        mreakString = mreakString + "< /" + methodName + "> ";
       
        String soap2 = "< /soap:Envelope> ";
        String requestData = soap + mreakString + soap2;
        //System.out.println(requestData);

        try {
            URL url = new URL(ServerUrl);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            byte[] bytes = requestData.getBytes("utf-8");
            con.setDoInput(true);
            con.setDoOutput(true);
            con.setUseCaches(false);
            con.setConnectTimeout(12000);// 设置超时时间
            con.setRequestMethod("POST");
            con.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
            con.setRequestProperty("SOAPAction", soapAction);
            con.setRequestProperty("Content-Length", "" + bytes.length);
            OutputStream outStream = con.getOutputStream();
            outStream.write(bytes);
            outStream.flush();
            outStream.close();
            InputStream inStream = con.getInputStream();

           // System.out.println(inStream);
            //data=parser(inStream);
            //System.out.print("11");
            Values = inputStreamtovaluelist(inStream, methodName);
            //System.out.println(Values.size());


        } catch (Ex ception e) {
            System.out.print("2221");

        }
        return Values;
    }

    public ArrayList< String>  inputStreamtovaluelist(InputStream in, String MonthsName) throws IOEx ception {
        StringBuffer out = new StringBuffer();
        String s1 = "";
        byte[] b = new byte[4096];
        ArrayList< String>  Values = new ArrayList< String> ();
        Values.clear();

        for (int n; (n = in.read(b)) != -1;) {
            s1 = new String(b, 0, n);
            out.append(s1);
        }

        System.out.println(out);
        String[] s13 = out.toString().split("> < ");// s1.split("> < ");
        String ifString = MonthsName + "Result";
        String TS = "";
        String vs = "";

        Boolean getValueBoolean = false;
        for (int i = 0; i <  s13.length; i++) {
            TS = s13[i];

            System.out.println(TS);
            int j, k, l;
            j = TS.indexOf(ifString);
            k = TS.lastIndexOf(ifString);

            if (j > = 0) {
                System.out.println(j);
                if (getValueBoolean == false) {
                    getValueBoolean = true;
                } else {

                }

                if ((j > = 0) && (k >  j)) {
                    System.out.println("FFF" + TS.lastIndexOf("/" + ifString));
                    //System.out.println(TS);
                    l = ifString.length() + 1;
                    vs = TS.substring(j + l, k - 2);
                    //System.out.println("fff"+vs);
                    Values.add(vs);
                    System.out.println("退出" + vs);
                    getValueBoolean = false;
                    return Values;
                }

            }
            if(TS.equals("string /")){
                Values.add(" "); continue;
            }
            if(TS.equals("string/")){
                Values.add(" "); continue;
            }
            if (TS.lastIndexOf("/" + ifString) > = 0) {
                getValueBoolean = false;
                return Values;
            }

            if ((getValueBoolean) && (TS.lastIndexOf("/" + ifString) <  0) && (j <  0)) {
                k = TS.length();
                //System.out.println(TS);
                vs = TS.substring(7, k - 8);
                //System.out.println("f"+vs);
                Values.add(vs);
            }

        }

        return Values;
    }

}


3 针对表user的操作  DBUtil.java

package com.example.webservice;

import java.io.IOEx ception;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class DBUtil {
    private ArrayList< String>  arrayList = new ArrayList< String> ();
    private ArrayList< String>  brrayList = new ArrayList< String> ();
    private ArrayList< String>  crrayList = new ArrayList< String> ();
    private HttpConnSoap Soap = new HttpConnSoap();
  public static Connection getConnection() {
        Connection con = null;
        try {
            //Class.forName("org.gjt.mm.mysql.Driver");
            //con=DriverManager.getConnection("jdbc:mysql://192.168.0.106:3306/test?useUnicode=true&characterEncoding=UTF-8","root","initial");
        } catch (Ex ception e) {
            //e.printStackTrace();
        }
        return con;
    }

  // 获取所有用户信息
    public List< HashMap< String, String> >  getAllUser() {
        List< HashMap< String, String> >  list = new ArrayList< HashMap< String, String> > ();
        arrayList.clear();
        brrayList.clear();
        crrayList.clear();
        crrayList = Soap.GetWebServre("selectAllUser", arrayList, brrayList);
        HashMap< String, String>  tempHash = new HashMap< String, String> ();
        tempHash.put("id", "id");
        tempHash.put("name", "name");
        tempHash.put("age", "age");
        list.add(tempHash);

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

        return list;
    }

  // 增加信息
    public void insertUser(String name, String age) {

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

        arrayList.add("name");
        arrayList.add("age");
        brrayList.add(name);
        brrayList.add(age);

        Soap.GetWebServre("insertOneUser", arrayList, brrayList);
    }

  // 删除一条货物信息
    public void deleteUser(String id) {

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

        arrayList.add("id");
        brrayList.add(id);

        Soap.GetWebServre("deleteOneUser", arrayList, brrayList);
    }

}



4 配置文件

4.1 主 activity_main.xml

< ?xml version="1.0" encoding="utf-8"?> 
< android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"> 

   < ListView
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="gone" > 
    < /ListView> 

    < Button
        android:id="@+id/btn_all"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        android:text="@string/btn_all" /> 

  < Button
        android:id="@+id/btn_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        app:layout_constraintRight_toRightOf="parent"
        android:text="@string/btn_add" /> 

    < Button
        android:id="@+id/btn_delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginTop="10dip"
        android:text="@string/btn_delete" /> 

   < TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" /> 



< /android.support.constraint.ConstraintLayout> 



4.2 显示全部用户 dialog_show.xml

< ?xml version="1.0" encoding="utf-8"?> 
< android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"> 

    < ListView
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:visibility="visible"> 
    < /ListView> 

 < Button
        android:id="@+id/btn_return"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"

        android:layout_marginTop="10dip"
        android:text="返回" /> 

  < /android.support.constraint.ConstraintLayout> 



4.3 具体显示的列表条目 adapter_item.xml

< ?xml version="1.0" encoding="utf-8"?> 
< TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants"
    android:gravity="center" > 
< TableRow
        android:id="@+id/classroom_detail_item_tableRow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center" > 

        < TextView
            android:id="@+id/txt_id"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:height="40dp"
            android:textSize="14sp" > 
        < /TextView> 

        < TextView
            android:id="@+id/txt_name"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:height="40dp"
            android:textSize="14sp" > 
        < /TextView> 

        < TextView
            android:id="@+id/txt_age"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:height="40dp"
            android:textSize="14sp" > 
        < /TextView> 

    < /TableRow> 

< /TableLayout> 


4.4 删除用户的对话框 dialog_delete.xml

< ?xml version="1.0" encoding="utf-8"?> 
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" > 
 
    < EditText
        android:id="@+id/idEditText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/delete_hint" > 

        < requestFocus /> 
    < /EditText> 

    < LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" > 

        < Button
            android:id="@+id/but_ok"
            android:layout_width="100dip"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dip"
            android:text="@string/confirm" /> 

        < Button
            android:id="@+id/but_ret"
            android:layout_width="100dip"
            android:layout_height="wrap_content"
            android:layout_marginLeft="40dip"
            android:text="@string/cancel" /> 
    < /LinearLayout> 

< /LinearLayout> 


4.5 增加用户的对话框 dialog_add.xml

< ?xml version="1.0" encoding="utf-8"?> 
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" > 

    < EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/add_hint1" > 

        < requestFocus /> 
    < /EditText> 

    < EditText
        android:id="@+id/editText2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/add_hint2"
        android:inputType="number" /> 

    < LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" > 

        < Button
            android:id="@+id/but_ok"
            android:layout_width="100dip"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dip"
            android:text="@string/confirm" /> 

        < Button
            android:id="@+id/but_ret"
            android:layout_width="100dip"
            android:layout_height="wrap_content"
            android:layout_marginLeft="40dip"
            android:text="@string/cancel" /> 
    < /LinearLayout> 

< /LinearLayout> 


5 字符串 strings.xml

< resources> 
    < string name="app_name"> WebService< /string> 
    < !-- string name="app_name"> book< /string>  --> 
    < string name="menu_settings"> Settings< /string> 
    < string name="title_activity_main"> MainActivity< /string> 
    < string name="btn_all"> 查看所有用户信息< /string> 
    < string name="btn_add"> 增加用户< /string> 
    < string name="btn_delete"> 删除用户< /string> 
    < string name="add_hint1"> 输入添加的用户名称< /string> 
    < string name="add_hint2"> 输入用户的年龄< /string> 
    < string name="confirm"> 确定< /string> 
    < string name="cancel"> 取消< /string> 
    < string name="delete_hint"> 输入删除的用户编号< /string> 
< /resources> 


六 执行效果

1 在模拟器上 主界面
AndroidStudio手机通过WebService远程访问SqlServer



显示所有用户,增加和删除略去





2 在真实手机上 显示所有用户


AndroidStudio手机通过WebService远程访问SqlServer



主界面,增加和删除略去







0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有