Android calls WebService's client implementation (II)

Source: Internet
Author: User

Original articles, reproduced please specify the Source: http://WWW.BLOG.CSDN.NET/TANGCHENG_OKto call WebService on Android, you have to need some libraries to support, in the example above, we access webservice through Xfire, but these libraries are not suitable for our Android client. Here is an open source project on Google Code KSOAP2-android,ksoap2-Android provides a lightweight and efficient soap library to access WebService. Download Ksoap2-android-assembly-2.6.0-jar-with-dependencies, and add it to the Android project, in this case just pass a simple string, the general use of WebService will use XML or JSON to share the data, as long as the specific needs, It is possible to parse the Envelope.bodyin data. WebService Service-side construction please refer to "Android Call WebService Server implementation (a)", nothing to say, directly on the code bar<span style= "FONT-SIZE:16PX;" > PackageOrg.winplus.aws; Importjava.io.IOException; ImportOrg.ksoap2.SoapEnvelope; ImportOrg.ksoap2.serialization.SoapObject; ImportOrg.ksoap2.serialization.SoapSerializationEnvelope; ImportOrg.ksoap2.transport.HttpTransportSE; Importorg.xmlpull.v1.XmlPullParserException; Importandroid.app.Activity; ImportAndroid.os.Bundle; ImportAndroid.util.Log;  Public classMainactivityextendsActivity {Private Final StaticString TAG = "Mainactivity"; //name Space    Private Static FinalString servicenamespace = "Http://192.168.217.1:8080/WebServiceDemo"; Private Static FinalString example = "Example"; //Request URL    Private Static FinalString serviceurl = "Http://192.168.217.1:8080/WebServiceDemo/services/HelloWorld"; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);            Setcontentview (R.layout.main); Try{log.e (TAG, Getrequestdata ()); } Catch(IOException e) {e.printstacktrace (); } Catch(xmlpullparserexception e) {e.printstacktrace (); }      }        /**      *       * @return      * @throwsIOException *@throwsxmlpullparserexception*/      PrivateString Getrequestdata ()throwsIOException, xmlpullparserexception {//instantiate the Soapobject object, specify the namespace of the webservice, and the name of the calling methodSoapobject request =NewSoapobject (Servicenamespace, example); //there is a string argument in the example method, where the "Android client" is passed to exampleRequest.addproperty ("message", "Android Client"); //get the serialized envelopeSoapserializationenvelope envelope; Envelope=NewSoapserializationenvelope (SOAPENVELOPE.VER11); Envelope.bodyout=request; //Android Transfer ObjectHttptransportse transport =NewHttptransportse (serviceurl); Transport.debug=true; //Call WebServiceTransport.call (Servicenamespace +example, envelope); if(Envelope.getresponse ()! =NULL) {              returnenvelope.bodyIn.toString (); }          return NULL; }  }  </span>function code is relatively simple, there is a call WebService analytic weather forecast example, interested to see it. SOURCE Download= = "

Android calls WebService's client implementation (II)

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.