Home Add Access database permissions in Androidmanifest.xml
<USES-SDK android:minsdkversion= "7"/>
<uses-permission android:name= "Android.permission.INTERNET"/>
Create a new class in SRC that joins WebService, with the name arbitrarily called "Httpconnsoap". Basically this class is fixed, to change the majority is the WebService port address, the specific code is as follows:
Package com.example.hospital;//name is changed to its own bag 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";//The majority of users to change is 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=\" 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 ();
Sets the parameter name of the method to a. NET WebService parameter
Vps=parvalues.get (i). toString ();
Ts= "<" +tps+ ">" +vps+ "</" +tps+ ">";
Mreakstring=mreakstring+ts;
}
mreakstring=mreakstring+ "</" +methodname+ ">";
/*
+ "+ "<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);//Set timeout time
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 ("Exit" +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;
}
}
need to create a new database access class that communicates and operates by httpconnsoap this class and the underlying database. The new method here will be with you new WebService, my webservice is built with Vs2010,.net 3.5 frame. Here are the two methods, the writing is probably the case, the user should write according to their own needs to write the good.
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 listlist
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, where the data is displayed with a ListView .
Private ListView ListView;
Private Dbutil Dbutil;
Private Simpleadapter adapter;
private void Setlistview () {
list
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 operates on SQL Server database via WebService