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;
}