in the previous article, we developed the custom service: using MyEclipse to develop WebService, today we invoke the custom WebService interface with the Android program:
In the browser input http://192.168.0.5:8080/TestService/MyServicePort?wsdl
Where Myserviceport is the url-pattern= "/myserviceport" in the Sun-jaxws.xml file on the WebService server side
Note that the 192.168.0.5 here is my native IP address and needs to be changed to its own IP address, such as:
3. Build the Android client:
Click Add EXTERNL JARS
Add ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar Package
Note that when the Android client calls WebService , it involves connecting to the network, so
It is important to note that when the server side returns a binary form, the
Soapobject object = (soapobject) envelope.getresponse ();
Otherwise
Soapobject object = (soapobject) envelope. Bodyin;
When the Android client passes multiple parameters to the androidservice Service-side method:
Specify the namespace of the webservice and call the method name soapobject RPC = new Soapobject (NAMESPACE, "add"); Set the two parameters that need to be passed in to call the WebService interface Mobilecode, UserId
1. Add Permission:<uses-permission android:name="Android.permission.INTERNET"/ >
2. Create a new thread and let those steps execute in the new thread
The key code is as follows:
Name space
String NAMESPACE = "http://webservice.test.liuhang.com/";
Method name to invoke String methodName = "SayHello";//endpointstring endPoint = "HTTP://192.168.0.5:8080/TESTSERVICE/MYSERVICEPORT?WSDL" ;//Specify the namespace of the WebService and the method name of the call Soapobject RPC = new Soapobject (NAMESPACE, methodName); Set the two parameters Mobilecode, UserId rpc.addproperty ("arg0", "hahaaa") that need to be passed in to call the WebService interface; Generates SOAP request information that calls the WebService method and specifies the version of SOAP, final soapserializationenvelope envelope = new Soapserializationenvelope ( SOAPENVELOPE.VER11); envelope.dotnet = False;envelope.bodyout = RPC; Envelope.setoutputsoapobject (RPC); Final Httptransportse transport = new Httptransportse (endPoint); <span style= "White-space:pre" ></span>new Thread () {public void run () {try {<span style= "White-space:pre "></span>//calls WebService Transport.call (null, envelope); if (Envelope.getresponse ()!=null) {//Gets the returned data soapobject object = (soapobject) Envelope.bodyin; String result= object.getproperty (0). toString (); Message message = new Message (); Bundle bundle = new bundle (); Bundle.putstring ("result", result); <span style= "White-space:pre" ></span> Message.setdata (bundle); message.what = 1;handler.sendmessage (message);} } catch (Exception e) {<span style= "White-space:pre" ></span>e.printstacktrace (); } <span style= "White-space:pre" ></SPAN>};}. Start ();
Source Download
Android Development calls Custom WebService