I. Introduction of WebService |
WebService is a SOAP-based protocol that enables communication between a Web server and a Web server, which is platform agnostic for delivering XML data using the SOAP protocol, and is an important solution for addressing communication between heterogeneous platforms, such as between the Java platform and the. NET Platform. Therefore, in the Web application has the pivotal role, many organizations and organizations have published the WebService on the respective platform (for example: Weather forecast, flight information, stock market etc.), so that any platform and customers can enjoy these services, of course, some pay.
Second, Android KSOAP2 components |
For Android to call WebService, there are two ways to write your own code mainly through the URL to get httpurlconnection to establish a connection with the webservice, and then I/O read and write transmission and data acquisition, And to get the data to parse XML, it is more troublesome. Another is the use of third-party components, more commonly used is ksoap2-android.
Ksoap2-android This open source component provides a lightweight and efficient SOAP class library for Android platforms to facilitate communication between Android and WebService
1. Environment construction
Ksoap2-android Project Address: Http://code.google.com/p/ksoap2-android/You can download the latest version of the jar and add the jar to the project.
I use it here is Ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar
2. Main steps of KSOAP2 use
1) Web service parameter Preparation
// WebService service address String URL = "Http:// Webservice.webxml.com.cn/webservices/mobilecodews.asmx "; // web service string namespace = "Http:// webxml.com.cn/"; // method name of the request service string methodName = "Getmobilecodeinfo"; // soap request address string soapactionaddress = "Http://WebXml.com.cn/getMobileCodeInfo";
2) Create Httptransportse, which can send requests
New Httptransportse (URL);
3) Create Soapobject, add the data to be transferred (information carrier)
New Soapobject (namespace,methodname); Soapobject.addproperty (name,value); // Add Data ... ..
4) Create the Soapserializationenvelope object, specify the XML version, and request the body
New= soapobject;envelope.setoutputsoapobject (soapobject);
5) Send the request, call the method in Webserivce
Httptransportse.call (soapactionaddress, envelope); // The information returned by the service is placed in the Bodyin attribute of the envelope
6) Get the data returned by the service
Soapobject object = (soapobject) Envelope.bodyin;
Third, the implementation of the case-by calling WebService to find the place of the mobile phone number |
The following results are performed:
Complete code implementation:
Public classMainactivityextendsActivity {///Cell phone attribution to the WebService parameter information Private Static FinalString namespaceaddress = "http://WebXml.com.cn/";Private Static FinalString urladdress= "Http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"; Private Static FinalString methodnameaddress = "Getmobilecodeinfo"; Private Static FinalString soapactionaddress = "Http://WebXml.com.cn/getMobileCodeInfo"; PrivateTextView teladdress =NULL; PrivateEditText Tel =NULL; PrivateButton btnaddress =NULL;protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Btnaddress= (Button) This. Findviewbyid (r.id.btnsearchaddress); Teladdress= (TextView) This. Findviewbyid (r.id.teladdress); Tel= (EditText) This. Findviewbyid (R.id.telno); Btnaddress.setonclicklistener (NewButton.onclicklistener () {@Override Public voidOnClick (View v) {NewThread (NewRunnable () { Public voidrun () {getteladdress (); }}). Start (); } });/*** Request WebService and get the return cell number attribution information*/ Public voidgetteladdress () {soapobject soapobject=NewSoapobject (namespaceaddress, methodnameaddress);//To Create a SOAP object//set properties that are passed to the server through the SOAP protocolSoapobject.addproperty ("Mobilecode", Tel.gettext (). toString ());//the phone number to querySoapobject.addproperty ("UserId", "" "); Soapserializationenvelope Envelope=NewSoapserializationenvelope (SOAPENVELOPE.VER11); Envelope.bodyout=Soapobject; Envelope.dotnet=true; Envelope.setoutputsoapobject (Soapobject); Httptransportse Httptransportse=NewHttptransportse (urladdress); Try { //Invoke ServiceHttptransportse.call (soapactionaddress, envelope); } Catch(Exception e) {e.printstacktrace (); } //get the data returned by the service, the cell phone attribution informationSoapobject Object =(Soapobject) Envelope.bodyin; Txtaddress= Object.getproperty (0). toString (); //send message to main thread successful, getteladdress function completedHandleraddress.sendemptymessage (0); } Handler handleraddress=NewHandler () { Public voidhandlemessage (Message msg) {teladdress.settext (txtaddress); Toast.maketext (mainactivity. This, "Get number Attribution Success" +txtaddress, Toast.length_long). Show (); } };}
Iv. attached: Common WebService service URL |
Cell Phone attribution to service
Http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx
Weather forecast Web Service, data from China Meteorological Bureau
Http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
IP address to:
Http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx
Chinese <-> English bidirectional translation WEB service:
Http://www.webxml.com.cn/WebServices/TranslatorWebService.asmx
Train timetable
Http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx
Flight Enquiry Service
Http://webservice.webxml.com.cn/webservices/DomesticAirline.asmx
China stock market Data WEB Service
Http://www.webxml.com.cn/WebServices/ChinaStockWebService.asmx
China TV program Preview
Http://www.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx
Use Android KSOAP2 to call WebService in Android