There are many methods to call Web Services in Android. We now use ksoap, which is the client package of SOAP web services. The current version of ksoap is 2. 0. one of its major advantages is its good compatibility with dotNET.
First download the ksoap Package file (), right-click the project in the Package Explorer of Eclispe, Build Path> Add Libraries, and find the ksoap2-android-assembly-2.4-jar-with-dependencies.jar to Add the reference. The Code is as follows:
Public class WSHelper {
Final static String WSUrl = "http: // xxx/WSUrl. asmx ";
Private static String namespace = "http://tempuri.org /";
/*************************************
* Obtain web services content
* @ Param url
* @ Param params
* @ Return
*************************************/
Public static String GetResponse (String method, List <BasicNameValuePair> params ){
Try {
String url = WSUrl;
SoapObject request = new SoapObject (namespace, method );
For (int I = 0, len = params. size (); I <len; I ++ ){
Request. addProperty (params. get (I). getName (), params. get (I). getValue ());
}
SoapSerializationEnvelope envelope =
New SoapSerializationEnvelope (SoapEnvelope. VER11 );
Envelope. dotNet = true;
Envelope. setOutputSoapObject (request );
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport (url );
AndroidHttpTransport. call (namespace + method, envelope );
SoapPrimitive result = (SoapPrimitive) envelope. getResponse ();
Return result. toString ();
} Catch (Exception e ){
Return "Error: calling the web services error ";
}
}
}
The call time code is as follows:
String method = "MethodName"; // method name
List <BasicNameValuePair> params = new ArrayList <BasicNameValuePair> ();
Params. add (new BasicNameValuePair ("userId", String. valueOf (1995 )));
Return WSHelper. GetResponse (method, params );
Multiple parameters can be defined here, And the return result is similar to a String.
Note that because we call Web sevices, the program must have the permission to access the network. Therefore, we need to add the definition of the permission to access the network in manifest section of AndroidManifest. xml:
<Uses-permission android: name = "android. permission. INTERNET"> </uses-permission>