Invoking a Web Service using an Android app
Tools Required:
Ksoap2-android
: HTTP://PAN.BAIDU.COM/S/1JGL6B10
Build path adds ksoap2-android to project engineering
First, import the Ksoap2-android package into the Libs directory, right-click Build path, add to build path
Click Project Project name, right-build path---Configure build path in front of ksoap2-android this package, click OK
Complete
To invoke a Web Service step using an Android app:
1. Create the Httptransportse object that is used to invoke the Web service operation
You can specify the URL of the WebService WSDL document by constructing the method
Httptransportse Transportse = new Httptransportse (serviceurl);
2. Create the Soapserializationenvelope object to generate the SOAP request information that invokes the Web service method
Soapserializationenvelope envelope = new Soapserializationenvelope (SOAPENVELOPE.VER10);
3. Create a Soapobject object that needs to pass in the namespace of the Web service to be called, the Web service method name when the object is created
Soapobject RPC = new Soapobject (namespace, methodName);
4. If a parameter needs to be passed to the Web service server side, you need to call the AddProperty (String name, object value) method of the Soapobject object to set the parameter
Rpc.addproperty ("Mobilecode", Phonenum), Rpc.addproperty ("UserId", "");
5. Call the Setoutputsoapobject () method of Soapserializationenvelope, or assign a value directly to the Bodyout property, creating the first two steps
Outgoing SOAP message body with Soapobject object set to Soapserializationenvelope
Envelope.bodyout = Rpc;envelope.setoutputsoapobject (RPC);
6. Invoke the call method of the object and invoke the remote Web Service with Soapserializationenvelope as a parameter
Transportse.call (soapaction, envelope);
7. After the call is complete, access the Bodyin property of the Soapserializationenvelope object, which returns a Soapobject object that represents the return message of the Web service
Parse the Soapobject object to get the return value of the calling Web Service
Soapobject object = (soapobject) Envelope.bodyin;
Number attribution to query:
Layout file:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" fill_parent "android:layout_height=" Fill_parent "Android:orienta tion= "vertical" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/activity_ Vertical_margin "> <textview android:layout_width=" wrap_content "android:layout_height=" Wrap_conten T "android:text=" Enter a mobile phone number "/> <edittext android:id=" @+id/phone_num "Android:layout_width=" fil L_parent "android:layout_height=" wrap_content "android:inputtype=" textphonetic "android:singleline=" t Rue "/> <button android:id=" @+id/query "android:layout_width=" Wrap_content "Android:layout_h eight= "wrap_content" android:text= "Query"/> < TextView android:id= "@+id/show" android:layout_width= "fill_parent" android:layout_height= "fill_parent "/></linearlayout>
Activity:
public class Mainactivity extends Activity {private EditText phonenum;private Button query;private TextView show;private S Tring result; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Phonenum = (EditText) Findviewbyid (r.id.phone_num); query = (Button) Findviewbyid (r.id.query); show = (TextView) Findviewbyid (r.id.show); final Handler Handler = new Handler () {@Overridepublic void Handlemessage (Message msg) {if (MSG.W hat = = 0x123) {show.settext (result);}}; Query.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {final String phonenum = Phonenum.gettext (). toString (). Trim (); new Thread () {public void run () {//namespace String namespace = "http://WebXml.com.cn/" ;//Method name string methodName = "Getmobilecodeinfo";//Serviceurlstring serviceurl = "http://webservice.webxml.com.cn/ Webservices/mobilecodews.asmx ";//soap Actionstring soapaction =" http://WebXml.com.cn/getMobileCodeInfo ";//Specify WebsErvice namespace and calling method Soapobject RPC = new Soapobject (namespace, methodName);//need to pass in parameter Rpc.addproperty ("Mobilecode", Phonenum); Rpc.addproperty ("UserId", "");//Generate SOAP request information that calls the WebService method and specify the version of Soap Soapserializationenvelope envelope = new Soapserializationenvelope (SOAPENVELOPE.VER10);//envelope.bodyout = rpc;// Sets whether to invoke Webserviceenvelope.dotnet = True;envelope.setoutputsoapobject (RPC) developed by DotNet; Httptransportse Transportse = new Httptransportse (serviceurl); try {transportse.call (soapaction, envelope);} catch ( Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();} Soapobject object = (soapobject) envelope.bodyin;//This method gets a key-value pair,//result = object.tostring (); result = Object.getproperty (0). toString (); Handler.sendemptymessage (0x123);};}. Start ();}});}}
Android------android app call Web service/number attribution query