Call Web Services in Android

Source: Internet
Author: User

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>

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.