Use Android Ksoap2 in Android to call WebService and androidksoap2

Source: Internet
Author: User

Use Android Ksoap2 in Android to call WebService and androidksoap2

1. Introduction to WebService

Based on the SOAP protocol, WebService can communicate between web servers and web servers. Because the XML data transmitted using the SOAP protocol is platform-independent, it is also an important solution for communication between heterogeneous platforms, for example, Java platform and.. net platform. Therefore, it plays an important role in web applications. Many organizations and organizations have released WebServices (such as weather forecasts, flight information, and stock market prices) on their respective platforms ), in this way, any platform and customers can enjoy these services, of course, some of which are charged.

Ii. Android ksoap2 component

There are two methods for Android to call WebService. One method is to write the code by yourself. You can use the URL to obtain HttpUrlConnection to establish a connection with webservice, and then perform I/O read/write transfer and data retrieval, it is troublesome to parse the obtained data in XML format. Another is to use third-party components, more commonly used is ksoap2-android.

The Open Source component of ksoap2-android provides a lightweight and efficient SOAP class library for the Android platform, which can facilitate the communication between Android and WebService.

1. Establish the environment

Ksoap2-android Project address: http://code.google.com/p/ksoap2-android/ you can download the latest version of jar, and then add the jar to the project.

I use ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar here

2. Main steps for using Ksoap2

1) Prepare web service parameters

// Webservice Service address String url = "canonical namespace =" http://webxml.com.cn/##//request Service's canonical name String methodName = "getMobileCodeInfo"; // soap request address String soapActionAddress = "http://WebXml.com.cn/getMobileCodeInfo ";

  2) create HttpTransportSE, which can send requests

HttpTransportSE transport = new HttpTransportSE(url);

  3) create SoapObject and add the data to be transferred (information carrier)

SoapObject soapObject = new SoapObject (namespace, methodName); soapObject. addProperty (name, value); // Add data...

4) create a SoapSerializationEnvelope object, specify the xml version, and the body in the request.

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);envelope.bodyOut = soapObject;envelope.setOutputSoapObject(soapObject);

5) send the request and call the method in webserivce.

HttpTransportSE. call (soapActionAddress, envelope); // The information returned by the service is stored in the bodyIn attribute of envelope.

6) obtain the data returned by the Service

SoapObject object = (SoapObject) envelope.bodyIn;
Iii. Implementation case -- query the location of the mobile phone number by calling webservice

The execution result is as follows:

 

Complete code implementation:

Public class MainActivity extends Activity {// parameter information private static final String nameSpaceAddress = "http://WebXml.com.cn/"; private static final String urlAddress = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx "; private static final String methodNameAddress = "getMobileCodeInfo"; private static final String soapActionAddress = "http://WebXml.com.cn/getMobileCodeInfo"; private TextView telAddress = null; private EditText tel = null; private Button btnAddress = null; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); btnAddress = (Button) this. findViewById (R. id. btnSearchAddress); telAddress = (TextView) this. findViewById (R. id. telAddress); tel = (EditText) this. findViewById (R. id. telNo); btnAddress. setOnClickListener (new Button. onClickListener () {@ Override public void onClick (View v) {new Thread (new Runnable () {public void run () {getTelAddress ();}}). start () ;}});/*** request WebService and obtain the returned mobile phone number home location information */public void getTelAddress () {SoapObject soapObject = new SoapObject (nameSpaceAddress, methodNameAddress ); // create a SOAP object // set attributes. These attributes are transmitted to the server soapObject through the SOAP protocol. addProperty ("mobileCode", tel. getText (). toString (); // The phone number soapObject to be queried. addProperty ("userId", ""); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope (SoapEnvelope. VER11); envelope. bodyOut = soapObject; envelope. dotNet = true; envelope. setOutputSoapObject (soapObject); HttpTransportSE httpTransportSE = new HttpTransportSE (urlAddress); try {// call the service httpTransportSE. call (soapActionAddress, envelope);} catch (Exception e) {e. printStackTrace ();} // gets the data returned by the service. The mobile phone uses the location information SoapObject object = (SoapObject) envelope. bodyIn; txtAddress = object. getProperty (0 ). toString (); // The message is successfully sent to the main thread. After the getTelAddress function is executed, handlerAddress is completed. sendEmptyMessage (0);} Handler handlerAddress = new Handler () {public void handleMessage (Message msg) {telAddress. setText (txtAddress); Toast. makeText (MainActivity. this, "succeeded in obtaining the number" + txtAddress, Toast. LENGTH_LONG ). show ();}};}
Iv. Appendix: Common WebService URLs

Mobile phone home location service

Http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx

Weather forecast Web service, data from China Meteorological Administration

Http://www.webxml.com.cn/WebServices/WeatherWebService.asmx

IP Address:

Http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx

Chinese <-> English two-way translation WEB service:

Http://www.webxml.com.cn/WebServices/TranslatorWebService.asmx

Train Timetable

Http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx

Flight query service

Http://webservice.webxml.com.cn/webservices/DomesticAirline.asmx

China Stock Market Data WEB Service

Http://www.webxml.com.cn/WebServices/ChinaStockWebService.asmx

Chinese TV program Preview

Http://www.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx

 

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.