Import java.io.IOException;
Import Org.ksoap2.SoapEnvelope;
Import Org.ksoap2.serialization.SoapObject;
Import Org.ksoap2.serialization.SoapSerializationEnvelope;
Import Org.ksoap2.transport.HttpTransportSE;
Import org.xmlpull.v1.XmlPullParserException;
Import Android.os.Bundle;
Import android.app.Activity;
Import Android.view.Menu;
Import Android.widget.EditText;
public class Callws extends Activity {
Final static String Service_ns = "http://lee/";
Final static String Service_url = "Http://172.18.5.198/crazyit";
Private EditText Text1;
Private EditText Text2;
@Override
protected void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (R.LAYOUT.ACTIVITY_CALL_WS);
Text1 = (EditText) Findviewbyid (R.ID.TEXT1);
Text2 = (EditText) Findviewbyid (R.ID.TEXT2);
//method to invoke
String methodName = "Getuserlist";
//Create Httptransportse Transport object
Httptransportse ht = new Httptransportse (service_url);
Ht.debug = true;
//Create envelop object using SOAP1.1 protocol
soapserializationenvelope envelope = new Soapserializationenvelope (SOAPENVELOPE.VER11);
//Instantiate Soapobject object
soapobject soapobject = new Soapobject (Service_ns, methodName);
soapobject.addproperty ("arg0", "Client Parameters:");
//Set the Soapobject object to an outgoing SOAP message for the Soapserializationenvelope object
envelope.bodyout = soapobject;
try {
//Call Web Service
Ht.call (null, envelope);
if (envelope.getresponse () = null) {
//Gets the SOAP message returned by the server response
soapobject result = (soapobject) envelope.bodyin;
///Next is the process of parsing the response data from the Soapobject object
soapobject detail1 = (soapobject) result.getproperty (0);
soapobject detail2 = (soapobject) result.getproperty (1);
StringBuilder person1 = new StringBuilder ();
person1.append ("User name:");
Person1.append (Detail1.getproperty (3));
person1.append ("\ n password");
person1.append (detail1.getproperty (0));
person1.append ("\ n height");
Person1.append (Detail1.getproperty (1));
Text1.settext (person1.tostring ());
StringBuilder person2 = new StringBuilder ();
person2.append ("User name:");
Person2.append (Detail2.getproperty (3));
person2.append ("\ n password");
person2.append (detail2.getproperty (0));
person2.append ("\ n height");
Person2.append (Detail2.getproperty (1));
Text2.settext (person2.tostring ());
}
} catch (IOException e) {
e.printstacktrace ();
} catch (Xmlpullparserexception e) {
e.printstacktrace ();
}
}
}
The bold code in the above program represents the 7 key steps to invoking a Web service operation using Ksoap2-android.
Regardless of how powerful the service is provided by the Remote Web service and how complex the business is, it is completely transparent to Android clients and the server returns a SOAP message containing the results as soon as the request parameters are sent. With the help of Web service, a very powerful application is implemented in Android applications-----Anyway, the specific business logic is implemented by the Web service server, as long as the Web Service service is invoked by the Android client.
To invoke an instance of a Web service operation through Ksoap2-android