Android Call WebService

Source: Internet
Author: User

Recently began to study the internet of Things, the first contact with Android development, learning to meet the need to call the PC-side WebService problem, now will learn the knowledge of their own records, in order to review in the future.

Preparatory work:

1 Download third-party class library KSOAP2.

2. Use Eclipse to create a new Android project and copy the downloaded KSOA2 into the Android project Lib folder.

3. Use VS2012 to create a new webservice and publish it on IIS

To invoke the WebService concrete steps:

1. Create a Soapobject object

New Soapobject (Namepace, methodName);

Namepace is a namespace, MethodName is the method to invoke

2. Set the parameters of the method to invoke, if the method has no parameters to fill

Request.addproperty ("param1", "value"); Request.addproperty ("param2", "value");

param is the parameter name, value is the argument value

3. Create the Soapserializationenvelope object and assign the Soapobject created in the first step to the Boduout property

New= soapobject;

Soapenvelope.ver10 is the version number of the soap, set according to the version number of the server

4. Create a Httptransportse object

New Httptransportse (URL);

WSDL document address with URL WebService

5. Calling the call method to get the return value  

Htse.call (Namepace + methodName, SSE)

The first argument to call is a string value for the namespace + method name, and the second parameter is the Soapserializationenvelope created by step three

6. Save the return value

Soapobject Soapresponse == soapresponse.getproperty (0). toString ();

Use the Bodyin property of the Soapserializationenvelope class to get the returned Soapobject and assign a value to string.

Note: 1. The method of accessing the network after Android4.0 can no longer be executed in the UI thread, a new thred is required

2. Add permission to access the network in the Androidmanifest folder

<uses-permission android:name= "Android.permission.INTERNET"/>

3. The child thread prevents the UI from being updated, so if you want to display the data returned in WebService in the interface you need to use handle and message

The next step is to show the Hellowrod method that calls the new WebService itself.

 Public classMainactivityextendsActivity {PrivateEditText Et_url; PrivateSoapobject soap; PrivateSoapobject Soapresponse; PrivateString namepace = "http://tempuri.org/"; PrivateString methodName = "HelloWorld"; PrivateString URL; PrivateSoapserializationenvelope SSE; PrivateString result; PrivateHandler Handler =NewHandler () { Public voidhandlemessage (Message msg) {((TextView) Findviewbyid (R.id.textview1)). SetText ((String) msg.obj);    };    }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);    Initview (); }     Public voidInitview () {Findviewbyid (r.id.button1). Setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Et_url=(EditText) Findviewbyid (R.id.et_url); URL=Et_url.gettext (). toString (); NewThread () { Public voidrun () {toservice ();                };            }.start ();    }        }); }     Public voidToservice () {Soap=NewSoapobject (Namepace, methodName); SSE=NewSoapserializationenvelope (SOAPENVELOPE.VER10); Sse.bodyout=soap; Sse.dotnet=true; Httptransportse Htse=NewHttptransportse (URL); Try{htse.call (namepace+MethodName, SSE); Soapresponse=(Soapobject) Sse.bodyin; Result= Soapresponse.getproperty (0). toString (); } Catch(IOException e) {e.printstacktrace (); } Catch(xmlpullparserexception e) {e.printstacktrace (); } Message msg=NewMessage (); Msg.obj=result;    Handler.sendmessage (msg); }}

Android Call 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.