Webservice calls in android

Source: Internet
Author: User

 

WebService is a SOAP-based Remote Call standard. It integrates different operating system platforms, languages, and technologies. The Android SDK does not provide a library to call WebService. Therefore, you must use a third-party SDK to call WebService. PC-based WEbservice client libraries are rich, such as Axis2 and CXF. However, these sdks are too large for Android systems and may not be easily transplanted to Android systems. Therefore, these sdks are not under our consideration. Some sdks are suitable for WebService clients of mobile phones, and Ksoap2 is commonly used. Reference this jar package in the Eclipse project.

 

The method for calling webservice is as follows:

 

(1) Specify the webservice namespace and the name of the called method, for example:

 

SoapObject request = new SoapObject (http: // service, "getName ");

 

 

The first parameter of the SoapObject class represents the WebService namespace. You can find the WebService namespace from the WSDL document. The second parameter indicates the name of the WebService method to be called.

 

(2) set the parameter value of the method to be called. If there is no parameter, it can be omitted. The code for setting the parameter value of the method is as follows:

 

Request. addProperty ("param1", "value"); Request. addProperty ("param2", "value ");

 

 

Note that although the 1st parameters of the addProperty method represent the parameter name of the method to be called, the parameter value is not necessarily the same as the method parameter name in the WebService class of the server, you only need to set parameters in the same order.

 

(3) generate the SOAP request information that calls the Webservice method. This information is described by the SoapSerializationEnvelope object. The code is:

 

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope (SoapEnvelope. VER11); Envelope. bodyOut = request;

 

 

When creating a SoapSerializationEnvelope object, you must use the SoapSerializationEnvelope class constructor to set the SOAP Protocol version number. This version number must be set according to the WebService version number on the server. After creating a SoapSerializationEnvelope object, do not forget to set the bodyOut attribute of the SOAPSoapSerializationEnvelope class. The value of this attribute is the SoapObject object created in step 1.

 

(4) Create an HttpTransportsSE object. You can use the HttpTransportsSE Class Construction Method to specify the URL of the WebService WSDL document:

 

HttpTransportSE ht = new HttpTransportSE ("http: // 192.168.18.17: 80/axis2/service/SearchNewsService? Wsdl ");

 

 

(5) use the call method to call the WebService method. Code:

 

Ht. call (null, envelope );

 

 

The first parameter of the Call method is generally null. The first parameter is the SoapSerializationEnvelope object created in step 1.

 

(6) use the getResponse method to obtain the returned results of the WebService method. Code:

 

SoapObject soapObject = (SoapObject) envelope. getResponse ();

 

 

The following is an example of a simple weather display function:

 

Public class WebService extends Activity {private static final String NAMESPACE = "http://WebXml.com.cn/"; // WebService address private static String URL = "http://www.webxml.com.cn/webservices/weatherwebservice. asmx "; private static final String METHOD_NAME =" getWeatherbyCityName "; private static String SOAP_ACTION =" http://WebXml.com.cn/getWeatherbyCityName "; private String weatherToday; private Button okButton; private SoapObject detail; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); okButton = (Button) findViewById (R. id. OK); okButton. setOnClickListener (new Button. onClickListener () {public void onClick (View v) {showWeather () ;}}) ;}private void showWeather () {String city = "Wuhan"; getWeather (city );} @ SuppressWarnings ("deprecation") public void getWeather (String cityName) {try {System. out. println ("rpc ------"); SoapObject rpc = new SoapObject (NAMESPACE, METHOD_NAME); System. out. println ("rpc" + rpc); System. out. println ("cityName is" + cityName); rpc. addProperty ("theCityName", cityName); AndroidHttpTransport ht = new AndroidHttpTransport (URL); ht. debug = true; SoapSerializationEnvelope envelope = new SoapSerializationEnvelope (SoapEnvelope. VER11); envelope. bodyOut = rpc; envelope. dotNet = true; envelope. setOutputSoapObject (rpc); ht. call (SOAP_ACTION, envelope); SoapObject result = (SoapObject) envelope. bodyIn; detail = (SoapObject) result. getProperty ("getWeatherbyCityNameResult"); System. out. println ("result" + result); System. out. println ("detail" + detail); Toast. makeText (WebService. this, detail. toString (), Toast. LENGTH_LONG ). show (); parseWeather (detail); return;} catch (Exception e) {e. printStackTrace ();

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.