android-connecting SQL Server database with WebService

Source: Internet
Author: User

What was done before, as long as the database is in the project to re-make a copy of the data. But this method is very undesirable, first of all, the cell phone memory is not very large, the data table built in the project undoubtedly increased the program. As a result, the mobile phone running speed imaginable. Secondly, when the data is large, the database is more suitable, not only convenient but also can achieve the effect of synchronization.

Many applications are dependent on the data in the database, the convenience of streamlining and synchronization to the database method only connected to the database. This is the purpose of obtaining database information by using WebService to connect the database as the SOAP protocol.

A small example was made:

Layout:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"Xmlns:tools="Http://schemas.android.com/tools"Android:layout_width="match_parent"Android:layout_height="match_parent"android:orientation="Vertical"> <TextView android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="@string/hello_world"Tools:context=". Mainactivity"/> <LinearLayout android:layout_width="wrap_content"Android:layout_height="wrap_content"android:orientation="Horizontal"> <EditText Android:id="@+id/name"Android:layout_width="200DP"Android:layout_height="wrap_content"/> <Button Android:id="@+id/search"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="@string/search"/> </LinearLayout> <textview android:id="@+id/result"Android:layout_width="fill_parent"Android:layout_height="wrap_content"/> </LinearLayout>

Build a tool class Soaputil:

 Public classSoaputil { Public StaticObject Dotransport (FinalString Wsdurl,FinalString WebMethod) {String NameSpace= "http://tempuri.org/";//is usually the defaultSoapobject Soapobject =NewSoapobject (NameSpace, WebMethod); //Soapobject.addproperty (propertyInfo)System.out.println (); Soapserializationenvelope Soapserializationenvelope=NewSoapserializationenvelope (SOAPENVELOPE.VER11); Soapserializationenvelope.bodyin=Soapobject; Soapserializationenvelope.dotnet=true;          Soapserializationenvelope.setoutputsoapobject (Soapobject); Httptransportse Httptransportse=NewHttptransportse (Wsdurl); String soap_action= "http://tempuri.org/" +WebMethod; //Output SOAPActionSystem.out.println (soap_action); Try{httptransportse.call (soap_action, Soapserializationenvelope); System.out.println ("End of Call"); //Output ResponseSystem.out.println (Soapserializationenvelope.getresponse ()); if(Soapserializationenvelope.getresponse ()! =NULL) {soapobject result=(Soapobject) soapserializationenvelope. GetResponse (); //Output Results                 for(inti = 0; I < Result.getpropertycount (); i++) {System.out.println ("Result [" + i + "] =" +Result.getproperty (i). toString ()); }                                     returnresult; }          } Catch(IOException e) {System.out.println ("IOException");          E.printstacktrace (); } Catch(xmlpullparserexception e) {e.printstacktrace (); }          return NULL; }  }

Main implementation methods:

 Public classMainactivityextendsActivity {PrivateButton searchs; PrivateTextView results; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);          Setcontentview (R.layout.activity_main); Searchs=(Button) Findviewbyid (R.id.search); Results=(TextView) Findviewbyid (R.id.result); Searchs.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {//Server AddressString wsdurl= "Http://192.168.1.195:88/service1.asmx"; //Method NameString method= "SelectAll"; Object result=Soaputil.dotransport (Wsdurl, method);                                 Results.settext (Result.tostring ());      }          }); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.activity_main, menu); return true; }  }

These are the necessary steps for us on the client side, in addition to the server-given interface (the interface name is the method name in the activity). Here I do not write interface, interface is actually very simple all kinds of programming language can be, mainly SQL operation statement, the deployment to the server can be completed.

android-connecting SQL Server database with WebService

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.