Android Development calls Custom WebService

Source: Internet
Author: User
Tags soap wsdl

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" &GT;&LT;/SPAN&GT;};}. Start ();

Source Download

Android Development calls Custom WebService

Related Article

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.