SoFunction
Updated on 2025-04-09

Android operates SQLServer database instance code through Webservice

Add access to database permissions in the home page

<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="" />

Create a new class connecting to the webservice in src, with the name of it at will, it is called "HttpConnSoap". Basically, this class is fixed, and most of the things to be changed are the webservice port address. The specific code is as follows:

package ;//The name should be changed to your own package nameimport ;
import ;
import ;
import ;
import ;
import ;
import ;
import .; 
import ; 
public class HttpConnSoap {
public ArrayList&lt;String&gt; GetWebServre(String methodName,ArrayList&lt;String&gt; Parameters,ArrayList&lt;String&gt;ParValues)
{
ArrayList&lt;String&gt; Values=new ArrayList&lt;String&gt;();
String ServerUrl="http://10.0.2.2:8093/";//The majority of netizens want to change are here.//String soapAction="/LongUserId1";
String soapAction="/"+methodName;
String data="";
String soap = "&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;" 
+ "&lt;soap:Envelope xmlns:xsi=\"http:///2001/XMLSchema-instance\" xmlns:xsd=\"http:///2001/XMLSchema\" xmlns:soap=\"/soap/envelope/\"&gt;" 
+"&lt;soap:Body /&gt;";
String tps,vps,ts;
String mreakString="";
mreakString="&lt;"+methodName+" xmlns=\"/\"&gt;";
for ( int i = 0; i &lt; (); i++) {
tps=(i).toString();
//Set the parameter of this method to the parameter name in the .net webServicevps=(i).toString(); 
ts="&lt;"+tps+"&gt;"+vps+"&lt;/"+tps+"&gt;";
mreakString=mreakString+ts;
}
mreakString=mreakString+"&lt;/"+methodName+"&gt;";
/*
+"&lt;HelloWorld xmlns=\"/\"&gt;"
+"&lt;x&gt;string11661&lt;/x&gt;"
+"&lt;SF1&gt;string111&lt;/SF1&gt;"
+ "&lt;/HelloWorld&gt;"
*/
String soap2="&lt;/soap:Envelope&gt;"; 
String requestData=soap+mreakString+soap2;
(requestData);
try{
URL url =new URL(ServerUrl);
HttpURLConnection con=(HttpURLConnection)();
byte[] bytes=("utf-8");
(true);
(true);
(false);
(8000);// Set the timeout time("POST");
("Content-Type", "text/xml;charset=utf-8");
("SOAPAction",soapAction);
("Content-Length",""+);
OutputStream outStream=();
(bytes);
();
();
InputStream inStream=();
//data=parser(inStream);
//("11");
Values= inputStreamtovaluelist(inStream,methodName);
//(());
return Values;
}
catch(Exception e)
{
("2221");
return null;
}
}
public ArrayList&lt;String&gt; inputStreamtovaluelist (InputStream in,String MonthsName) throws IOException { 
StringBuffer out = new StringBuffer();
String s1="";
byte[] b = new byte[4096]; 
ArrayList&lt;String&gt; Values=new ArrayList&lt;String&gt;();
();
for (int n; (n = (b)) != -1;) { 
s1=new String(b, 0, n);
(s1);
} 
(out);
String[] s13=("&gt;&lt;");
String ifString=MonthsName+"Result";
String TS="";
String vs="";
Boolean getValueBoolean=false;
for(int i=0;i&lt;;i++){
TS=s13[i];
(TS);
int j,k,l;
j=(ifString);
k=(ifString);
if (j&gt;=0)
{
(j);
if (getValueBoolean==false)
{
getValueBoolean=true;
}
else {
}
if ((j&gt;=0)&amp;&amp;(k&gt;j))
{
("FFF"+("/"+ifString));
//(TS);
l=()+1;
vs=(j+l,k-2);
//("fff"+vs);
(vs);
("quit"+vs);
getValueBoolean=false;
return Values; 
}
}
if (("/"+ifString)&gt;=0)
{
getValueBoolean=false;
return Values; 
}
if ((getValueBoolean)&amp;&amp;(("/"+ifString)&lt;0)&amp;&amp;(j&lt;0))
{
k=();
//(TS);
vs=(7,k-8);
//("f"+vs);
(vs);
}
}
return Values; 
}
}

You need to create a new database access class, and communicate with the underlying database through the HttpConnSoap class. The new method here is always the same as when you create a new website. My website is built using the vs2010, .net 3.5 framework. Here are two methods. This is probably the way to write. When netizens write, they should write according to their own needs.

public class DBUtil 
{
static boolean feeflag=false;
ArrayList&lt;String&gt; arrayList=new ArrayList&lt;String&gt;();
ArrayList&lt;String&gt; brrayList=new ArrayList&lt;String&gt;();
ArrayList&lt;String&gt; crrayList=new ArrayList&lt;String&gt;();
HttpConnSoap Soaptest=new HttpConnSoap(); 
public static Connection getConnection()
{
Connection con=null; 
try
{
("111");
("");
("222");
con=("jdbc:mysql://192.168.0.100:3306/test?useUnicode=true&amp;characterEncoding=UTF-8","root","123456");
("333");
}
catch(Exception e)
{
("444");
();
}
return con;
}
//Inquiry of student informationpublic String[] selectStu(String StuNO)
{
String ss[]=new String[8];
String result=null;
();
();
();
("StuNO");
(StuNO);
crrayList=("selectStu", arrayList, brrayList);
ss[0]=(0);
ss[1]=(1);
ss[2]=(2);
ss[3]=(3);
ss[4]=(4);
ss[5]=(5);
ss[6]=(6);
ss[7]=(7);
return ss;
}
public List&lt;HashMap&lt;String, String&gt;&gt; selectStuAll() {
List&lt;HashMap&lt;String, String&gt;&gt; list = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
();
();
();
crrayList = ("selectStuAll", arrayList, brrayList);
/*HashMap<String, String> tempHash = new HashMap<String, String>();
 ("S_Name", "Name");
 ("S_Age", "Age");
 ("S_Sex", "Gender");
 (tempHash);*/
for (int j = 0; j &lt; (); j += 3) {
HashMap&lt;String, String&gt; hashMap = new HashMap&lt;String, String&gt;();
("S_Name", (j));
("S_Age", (j + 1));
("S_Sex", (j + 2));
(hashMap);
}
return list;
}
}

The following is called in an Android program, and the data is displayed using listview here.

private ListView listView;
private DBUtil dbUtil;
private SimpleAdapter adapter;
private void setListView() {
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
list = ();
adapter = new SimpleAdapter(
, 
list, 
, 
new String[] { "S_Name", "S_Age", "S_Sex" }, 
new int[] { .textView1, .textView2, .textView3 });
(adapter);
}

I will introduce so much to Android's relevant knowledge on operating the SQLserver database through Webservice. I will introduce it to you in the future. I hope you will continue to pay attention to this site. Thank you.