没有合适的资源?快使用搜索试试~ 我知道了~
Android通过Webservice操作sqlserver数据库实例代码
9 下载量 81 浏览量
2020-09-02
22:23:25
上传
评论
收藏 49KB PDF 举报
温馨提示
试读
3页
主要介绍了Android通过Webservice操作sqlserver数据库的相关知识,对webservice操作数据库相关知识感兴趣的朋友一起学习吧
资源推荐
资源详情
资源评论
Android通过通过Webservice操作操作sqlserver数据库实例代码数据库实例代码
主要介绍了Android通过Webservice操作sqlserver数据库的相关知识,对webservice操作数据库相关知识感兴趣的朋友一起学习吧
首页在AndroidManifest.xml中添加访问数据库权限
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.INTERNET" />
在src中新建一个连接webservice的类,名字随意,这里叫做“HttpConnSoap”。基本上这个类是固定的,要改的大多数就是webservice端口地址,具体代码如
下:
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++){
资源评论
weixin_38588394
- 粉丝: 8
- 资源: 954
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功