Android通过Webservice操作sqlserver数据库实例代码
程序员文章站
2024-02-17 08:39:34
首页在androidmanifest.xml中添加访问数据库权限
...
首页在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++){ 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框架搭建的。这里列举两个方法,写法大概就是这样的,网友写的时候要根据自己的需求来写就好了。
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显示数据。
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数据库的相关知识,就给大家介绍这么多,后续还会给大家介绍相关知识,希望大家持续关注本站,谢谢。
上一篇: iOS通过代理逆向传值的方式详解
下一篇: 基于C#的音乐播放器主Form实现代码