What is WebService?
WebService is a remote invocation standard based on the SOAP protocol, and WebService can integrate different operating system platforms, different languages and different technologies into one piece. The library that calls WebService is not available in the Android SDK, so you need to use a third-party SDK to call WebService. The PC version of the WebService client library is very rich, such as AXIS2,CXF, but these development packages are too large for Android and may not be easily ported to Android systems. Therefore, these development packages are not within our scope of consideration. WebService Client SDK for mobile phone has some, more commonly used KSOAP2, can be downloaded from http://code.google.com/p/ksoap2-android/downloads/list , copy the downloaded Ksoap2-android-assembly-2.4-jar-with-dependencies.jar package to the Lib directory of the Eclipse Project and, of course, the other directory. This jar package is also referenced in the Eclipse project.
A specific invocation of the WebService method is:
1, first, call the WebService method requires an interface of three conditions, respectively:
' Namespace ', ' address ', and method name
The first two can be found in the WSDL document, targetnamespace and URL addresses
2.
Soapobject Request =new Soapobject (namespace, interface method name);
3. Sets the parameter value of the calling method, which can be omitted if the method has no arguments. The 1th parameter of the AddProperty method, while representing the parameter name of the calling method, is not necessarily the same as the method parameter name in the WebService class on the server side, as long as the parameters are set in the same order.
Request.addproperty ("username", name); Request.addproperty ("Password", pwd);
4 . When creating a Soapserializationenvelope object, the version number of the SOAP protocol needs to be set by the Soapserializationenvelope class's construction method. This version number needs to be set according to the version number of the server webservice. After you create the Soapserializationenvelope object, do not forget to set the Bodyout property of the Soapsoapserializationenvelope class, which is the Soapobject object created in the first step.
Soapserialzationenvelope envelope = new Sopserialzationenvelope (SOAPENVELOPE.VER11); envelope.bodyout = Request; Envelope.dotnet = True;envelope.setoutputsoapobject (request);
5 . Create a Httptransportsse object. You can specify the URL of the WSDL document for the WebService by using the Httptransportsse class's construction method:
Androidhttptransport transport = new Androidhttptransport (URL); transport.debug = true;
6. Invoke the WebService method using the call method, Parameter 1: namespace + method Name, Parameter 2:envelope object
Transport.call (namespace+smethodname, envelope);
7, using the GetResponse method to obtain the return result of the WebService method
Soapobject result = (soapobject) Envelope.bodyin;sreturn = (String) result.getproperty (0). toString ();
Here is an example of a simple implementation:
/** namespace */private final String NAMESPACE = "http://webservice.basic.com/";/** URL */private final String URL = "http://192 .168.1.213/helloservice?wsdl ";/** * WebService interface Call */private string Getwebservice (String name,string pwd) {//return value string Sreturn = "None!" ";//Method name string smethodname =" LOGINRPC ";//Specify the WebService namespace and the method name of the call Soapobject request = new Soapobject (NAMESPACE, Smetho DNAME);//Set the parameter value of the calling method, if the method has no parameters, you can omit Request.addproperty ("username", name); Request.addproperty ("Password", pwd);// Generates a SOAP request message that calls the WebService method Soapserializationenvelope envelope = new Soapserializationenvelope (SOAPENVELOPE.VER11); Envelope.bodyout = Request;envelope.dotnet = True;envelope.setoutputsoapobject (request);//Create Httptransportse object. @SuppressWarnings ("deprecation") androidhttptransport transport = new Androidhttptransport (URL); Transport.debug = true; try {//Use the Call method to invoke the WebService method Transport.call (Namespace+smethodname, envelope); Get the return result of the WebService method using the GetResponse method Soapobject result = (soapobject) Envelope.bodyin; Sreturn = (String) result.getproperty (0). toString (); } catch (Exception e) {e.printstacktrace (); }return Sreturn;}
Android Call Webservice-fengkun