Call Web Services in Android

Source: Internet
Author: User
Tags dotnet

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>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.