服务器之家

服务器之家 > 正文

Android通过Webservice操作sqlserver数据库实例代码

时间:2021-05-11 17:13     来源/作者:hyyweb

首页在AndroidManifest.xml中添加访问数据库权限

?
1
2
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.INTERNET" />

在src中新建一个连接webservice的类,名字随意,这里叫做“HttpConnSoap”。基本上这个类是固定的,要改的大多数就是webservice端口地址,具体代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.example.hospital;//名字要改成自己的包名
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import android.util.Xml;
public class HttpConnSoap {
public ArrayList<String> GetWebServre(String methodName,ArrayList<String> Parameters,ArrayList<String>ParValues)
{
ArrayList<String> Values=new ArrayList<String>();
String ServerUrl="http://10.0.2.2:8093/Service1.asmx";//网友要改的大多数就是这里。
//String soapAction="http://tempuri.org/LongUserId1";
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+">";
/*
+"<HelloWorld xmlns=\"http://tempuri.org/\">"
+"<x>string11661</x>"
+"<SF1>string111</SF1>"
+ "</HelloWorld>"
*/
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(8000);// 设置超时时间
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();
//data=parser(inStream);
//System.out.print("11");
Values= inputStreamtovaluelist(inStream,methodName);
//System.out.println(Values.size());
return Values;
}
catch(Exception e)
{
System.out.print("2221");
return null;
}
}
public ArrayList<String> inputStreamtovaluelist (InputStream in,String MonthsName) throws IOException {
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=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.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;
}
}

需要新建一个数据库访问类,通过HttpConnSoap这个类和底层数据库进行通信,操作。这里新建的方法要和你新建webservice时一直,我的webservice是用vs2010,.net 3.5框架搭建的。这里列举两个方法,写法大概就是这样的,网友写的时候要根据自己的需求来写就好了。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
public class DBUtil
{
static boolean feeflag=false;
ArrayList<String> arrayList=new ArrayList<String>();
ArrayList<String> brrayList=new ArrayList<String>();
ArrayList<String> crrayList=new ArrayList<String>();
HttpConnSoap Soaptest=new HttpConnSoap();
public static Connection getConnection()
{
Connection con=null;
try
{
System.out.println("111");
Class.forName("org.gjt.mm.mysql.Driver");
System.out.println("222");
con=DriverManager.getConnection("jdbc:mysql://192.168.0.100:3306/test?useUnicode=true&characterEncoding=UTF-8","root","123456");
System.out.println("333");
}
catch(Exception e)
{
System.out.println("444");
e.printStackTrace();
}
return con;
}
//查询学生信息
public String[] selectStu(String StuNO)
{
String ss[]=new String[8];
String result=null;
arrayList.clear();
brrayList.clear();
crrayList.clear();
arrayList.add("StuNO");
brrayList.add(StuNO);
crrayList=Soaptest.GetWebServre("selectStu", arrayList, brrayList);
ss[0]=crrayList.get(0);
ss[1]=crrayList.get(1);
ss[2]=crrayList.get(2);
ss[3]=crrayList.get(3);
ss[4]=crrayList.get(4);
ss[5]=crrayList.get(5);
ss[6]=crrayList.get(6);
ss[7]=crrayList.get(7);
return ss;
}
public List<HashMap<String, String>> selectStuAll() {
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
arrayList.clear();
brrayList.clear();
crrayList.clear();
crrayList = Soaptest.GetWebServre("selectStuAll", arrayList, brrayList);
/*HashMap<String, String> tempHash = new HashMap<String, String>();
tempHash.put("S_Name", "姓名");
tempHash.put("S_Age", "年龄");
tempHash.put("S_Sex", "性别");
list.add(tempHash);*/
for (int j = 0; j < crrayList.size(); j += 3) {
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("S_Name", crrayList.get(j));
hashMap.put("S_Age", crrayList.get(j + 1));
hashMap.put("S_Sex", crrayList.get(j + 2));
list.add(hashMap);
}
return list;
}
}

下面就是Android程序中调用了,这里用listview显示数据。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
private ListView listView;
private DBUtil dbUtil;
private SimpleAdapter adapter;
private void setListView() {
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
list = dbUtil.selectStuAll();
adapter = new SimpleAdapter(
DayList.this,
list,
R.layout.adapter,
new String[] { "S_Name", "S_Age", "S_Sex" },
new int[] { R.id.textView1, R.id.textView2, R.id.textView3 });
listView.setAdapter(adapter);
}

Android通过Webservice操作sqlserver数据库的相关知识,就给大家介绍这么多,后续还会给大家介绍相关知识,希望大家持续关注本站,谢谢。

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部