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.CodeAs follows:
Public class wshelper {final static string wsurl = "http: // xxx/wsurl. asmx "; Private Static string namespace =" http://tempuri.org /"; /*********************************** get 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 = 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 SevicesProgramYou have the permission to access the network. Therefore, you need to add the definition of the network access permission in the manifest section of androidmanifest. xml:
<Uses-Permission Android: Name = "android. Permission. Internet"> </uses-Permission>