Android uses ksoap2 to call the WebService Function Method in C #

Source: Internet
Author: User

I. Introduction to WebService

So what is WebService ?, It is a remote call standard based on the saop protocol. WebService can be used to integrate different operating system platforms, languages, and technologies.

2. add permissions to adroidmanifest. xml

  <! -- Access Network permissions -->

<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"

<Uses-Permission Android: Name = "android. Permission. Internet"/>
</Manifest>

3. Import the ksoap2 package

First download the kSOAP package: copy the ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar package to the Lib file under the project

In the android project, right-click and choose build path> Configure build path to add external archive. Then, select ksoap2.

4. Write client code for Android

Import package Library:

Import org. ksoap2.soapenvelope;
Import org. ksoap2.serialization. soapobject;
Import org. ksoap2.serialization. soapserializationenvelope;
Import org. ksoap2.transport. httptransportse;

Function Method:

Try {
Final string server_url = "http: // 124.205.154.198: 8081/service. asmx ";
String namespace = "http://tempuri.org/"; // namespace
String methodname = "sum"; // method name
String soapaction = "http://tempuri.org/Sum"; // helloworld namespace/method name
// Create a soapobject instance
Soapobject request = new soapobject (namespace, methodname );
Request. addproperty ("A", "G"); // this parameter is passed. Of course, you don't have to write this parameter if you don't want it.
// Request. addproperty ("passonstring", "rajapandian"); // this parameter is passed
// Generate a SOAP request message that calls the Web Service Method
Soapserializationenvelope envelope = new soapserializationenvelope (soapenvelope. ver11 );
Envelope. DOTNET = true; // set. NET Web Service
Envelope. setoutputsoapobject (request); // send a request
Httptransportse androidhttptransport = new httptransportse (server_url );
Androidhttptransport. Call (soapaction, envelope );
Object result = (object) envelope. getresponse ();
// Textview. settext (E. getmessage ());
Textview. settext (result. tostring ());
// Textview. settext ("7 ");
New alertdialog. Builder (this). settitle ("hint"). setmessage (result. tostring (). setpositivebutton ("OK", null). Show ();
}
Catch (exception E)
{
System. Out. println (E. getmessage ());
Textview. settext (E. getmessage ());
New alertdialog. Builder (this). settitle ("hint"). setmessage (E. getmessage (). setpositivebutton ("OK", null). Show ();
}

V. WebService Method

[Webmethod]
Public String sum (string)
{

String c = a + "Hello android ";
Return C;
}

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.