1. Download the KSOAP2 and copy the jar package to the Libs directory. Then right click on the copy in Jar and click on the add as Library in the popup menu.
2. Add permission to access the network in Androidmanifest.xml
<uses-permission android:name= "Android.permission.INTERNET"/>
<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.company.webservicedemo"> <uses-permissionAndroid:name= "Android.permission.INTERNET"/> <ApplicationAndroid:allowbackup= "true"Android:icon= "@mipmap/ic_launcher"Android:label= "@string/app_name"Android:supportsrtl= "true"Android:theme= "@style/apptheme"> <ActivityAndroid:name=". Mainactivity "Android:label= "@string/app_name"Android:theme= "@style/apptheme.noactionbar"> <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter> </Activity> </Application></Manifest>
3. Simple implementation of the layout
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"App:layout_behavior= "@string/appbar_scrolling_view_behavior"Tools:context= "Com.company.webservicedemo.MainActivity"Tools:showin= "@layout/activity_main"> <EditTextAndroid:id= "@+id/orgcode_txt"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "Please enter the organization code to query" /> <ButtonAndroid:id= "@+id/get_btn"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Get organization Information" /> <TextViewAndroid:id= "@+id/show_txt"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" /></LinearLayout>
4. A simple interface that is implemented by itself.
5. Click the Get button to invoke the interface and display the returned results in the TextView space.
PackageCom.company.webservicedemo;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.TextView;
Public classMainactivityextendsappcompatactivity {button button; TextView TextView; EditText Orgcodetxt;
@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button=(Button) Findviewbyid (R.ID.GET_BTN); TextView=(TextView) Findviewbyid (r.id.show_txt); Orgcodetxt=(EditText) Findviewbyid (r.id.orgcode_txt);
Button.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {webserviceutils.getorginfo (Orgcodetxt.gettext (). toString (),NewWebserviceutils.callback () {@Override Public voidresult (String result) {Textview.settext (result); } }); } }); }}
6. Call the specific implementation of the WebService interface, the main thread of the UI can not directly network communication, need to be completed in the child thread. The information returned is in a child thread and requires the use of handler to implement the delivery of the child thread and the main thread information.
PackageCom.company.webservicedemo;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportOrg.ksoap2.SoapEnvelope;ImportOrg.ksoap2.SoapFault;ImportOrg.ksoap2.serialization.SoapObject;ImportOrg.ksoap2.serialization.SoapSerializationEnvelope;ImportOrg.ksoap2.transport.HttpTransportSE; Public classWebserviceutils { Public InterfaceCallBack {voidresult (String result); } Public Static voidGetorginfo (FinalString Orgcode,FinalCallBack CallBack) { //handler for child threads to communicate with the main thread FinalHandler Mhandler =NewHandler () {@Override Public voidhandlemessage (Message msg) {Super. Handlemessage (msg); //callback The return value into the parameters of the callbackCallback.result ((String) msg.obj); } }; NewThread (NewRunnable () {@Override Public voidrun () {//name SpaceString nameSpace = "http://tempuri.org/"; //called method nameString methodName = "Getorginfo"; //EndPointString endPoint = "HTTP://192.168.1.12:8037/IWMSSERVICE.ASMX?WSDL"; //SOAP Action FinalString soapaction = "Http://tempuri.org/GetOrgInfo";//Namespace+methodname//establishing a WebService Connection object FinalHttptransportse transport =NewHttptransportse (EndPoint); //Transport.debug = true;//Whether it is debug mode//Setting Connection ParametersSoapobject Soapobject =NewSoapobject (NameSpace, methodName); Soapobject.addproperty ("Orgcode", Orgcode); //Set return parameters FinalSoapserializationenvelope envelope =NewSoapserializationenvelope (SOAPENVELOPE.VER11);//The SOAP protocol version must be SOAPENVELOPE.VER11 (soap V1.1)Envelope.dotnet =true;//Note: This property is supported for the Dotnetwebservice protocol if dotnet WebService needs to be set to trueEnvelope.bodyout = Soapobject;//Never mind!! Envelope.setoutputsoapobject (Soapobject);//Set Request Parameters Try{Transport.call (soapaction, envelope);//Call WebService}Catch(Exception e) {mhandler.sendmessage (Mhandler.obtainmessage (-1, E.getmessage ())); } if(Envelope.bodyininstanceofSoapFault) {SoapFault Error=(SoapFault) Envelope.bodyin; //sends an exception message to the main thread using handlerMhandler.sendmessage (mhandler.obtainmessage (0, Error.tostring ())); } Else{Soapobject Object= (soapobject) Envelope.bodyin;//get the returned data//send results to the main thread using handlerMhandler.sendmessage (Mhandler.obtainmessage (1, object.getproperty (0). toString ())); }}). Start (); }}
Android uses KSOAP2 tool class to implement WebService network programming