Android uses webservice to operate sqlserver databases. androidwebservice

Source: Internet
Author: User
Tags vps

Android uses webservice to operate sqlserver databases. androidwebservice

On the homepage, add database access permissions in AndroidManifest. xml.

<Uses-sdk android: minSdkVersion = "7"/>
<Uses-permission android: name = "android. permission. INTERNET"/>

Create a new webservice connection class in src. The class name is "HttpConnSoap ". Basically, this class is fixed. Most of the changes are webservice port addresses. The specific code is as follows:

Package com. example. hospital; // change the package name to your own package name.

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"; // most of the changes you want to change are here.
// 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 = \" external "xmlns: xsd = \" http://www.w3.org/2001/XMLSchema\ "xmlns: soap = \" external ">"
+ "<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 ();
// Set the parameter of this method to the parameter name in. net webService.
Vps = ParValues. get (I). toString ();
Ts = "<" + tps + ">" + vps + "</" + tps + "> ";
MreakString = mreakString + ts;
}
MreakString = mreakString + "</" + methodName + "> ";
/*
+ "<HelloWorld xmlns = \" http://tempuri.org/\ ">"
+ "<X> string000061 </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); // set the timeout value.
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" + );
Values. add ();
System. out. println ("quit" + );
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" + );
Values. add ();
}

}

Return Values;
}

}

You need to create a new database category class to communicate with the underlying database through the HttpConnSoap class. The method created here is consistent with that created when you created a webservice. My webservice was built using the vs2010 and. net 3.5 frameworks. Here we will list two methods. This is probably the way they are written. You should write them according to your own needs.

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;
}

// Query Student Information
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", "name ");
TempHash. put ("S_Age", "Age ");
TempHash. put ("S_Sex", "gender ");
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;
}

}

The following is called in the Android program. The listview is used to display data.

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 );

}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.