1. The jar package used is: ksoap2-android-assembly-2.4-jar-with-dependencies.jar
2. Results
3. Code:
Note:
A. Because android4.0 and later cannot make HTTP requests in the main thread, you need to add the following code to the oncreate function of acitivity.
if (Build.VERSION.SDK_INT >= 11) { StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads ().detectDiskWrites().detectNetwork().penaltyLog().build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build()); }
B. Add the network access permission and modify the minimum version in androidmanifest.
<uses-sdk android:minSdkVersion="11" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.INTERNET"></uses-permission>
The activity_main.xml code is as follows:
<Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns: Tools = "http://schemas.android.com/tools" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: paddingbottom = "@ dimen/activity_vertical_margin" Android: paddingleft = "@ dimen/plugin" Android: paddingright = "@ dimen/plugin" Android: paddingtop = "@ dimen/activity_v Ertical_margin "tools: context = ". mainactivity "> <edittext Android: Id =" @ + ID/edittextnum "Android: layout_width =" fill_parent "Android: layout_height =" wrap_content "Android: layout_alignparentleft =" true "Android: layout_alignparenttop = "true" Android: layout_margintop = "32dp" Android: EMS = "10"> <requestfocus/> </edittext> <button Android: id = "@ + ID/btnsearch" Android: layout_width = "fill_parent" Android: Layout _ Height = "wrap_content" Android: layout_alignleft = "@ + ID/edittext1" Android: layout_below = "@ + ID/edittextnum" Android: layout_margintop = "42dp" Android: TEXT = "query"/> <textview Android: Id = "@ + ID/textviewresult" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: layout_alignleft = "@ + ID/btnsearch" Android: layout_centervertical = "true" Android: text = "" Android: textappearance = "? Android: ATTR/textappearancemedium "/> </relativelayout>
Mainactivity. Java code:
Package cuit.edu. searchphonedemo; import Org. ksoap2.soapenvelope; import Org. ksoap2.serialization. soapobject; import Org. ksoap2.serialization. soapserializationenvelope; import Org. ksoap2.transport. httptransportse; import android. OS. build; import android. OS. bundle; import android. OS. strictmode; import android. app. activity; import android. view. menu; import android. view. view; import android. view. view. onclicklist Ener; import android. widget. button; import android. widget. edittext; import android. widget. textview; import android. widget. toast; public class mainactivity extends activity {private button searchbutton; private edittext numedittext; private textview resulttextview; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); searchbu Tton = (button) findviewbyid (R. id. btnsearch); numedittext = (edittext) findviewbyid (R. id. edittextnum); resulttextview = (textview) findviewbyid (R. id. textviewresult); searchbutton. setonclicklistener (New searchbtnonclicklistener (); If (build. version. sdk_int> = 11) {strictmode. setthreadpolicy (New strictmode. threadpolicy. builder (). detectdiskreads (). detectdiskwrites (). detectnetwork (). penaltylog (). build ()); Strictmode. setvmpolicy (New strictmode. vmpolicy. builder (). detectleakedsqlliteobjects (). detectleakedclosableobjects (). penaltylog (). penaltydeath (). build () ;}@ overridepublic Boolean oncreateoptionsmenu (menu) {// inflate the menu; this adds items to the action bar if it is present. getmenuinflater (). inflate (R. menu. main, menu); Return true;} class searchbtnonclicklistener implements onclicklistene R {@ overridepublic void onclick (view arg0) {// todo auto-generated method stubstring phonenum = numedittext. gettext (). tostring (). trim (); If ("". equals (phonenum) | phonenum. length () <7) {// The input does not conform to the toast. maketext (getapplicationcontext (), "The entered mobile phone number is invalid! ", Toast. length_long ). show (); numedittext. requestfocus (); return;} resulttextview. settext (getremoteinfo (phonenum);} Public String getremoteinfo (string phonesec) {// namespace string namespace = "http://WebXml.com.cn /"; // call method name string methodname = "getmobilecodeinfo"; // endpoint string endpoint = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"; // soap action string soapaction = "http://WebXml.com.cn/getMobileCodeInfo "; // specify the WebService namespace and method name soapobject RPC = new soapobject (namespace, methodname); // set the parameters mobilecode and userid that need to be passed in to call the WebService interface, it cannot be written at will. It must be the same name as the provided parameter RPC. addproperty ("mobilecode", phonesec); RPC. addproperty ("userid", ""); // generate the SOAP request information that calls the WebService method, and specify the soap version soapserializationenvelope envelope = new soapserializationenvelope (soapenvelope. ver11); envelope. bodyout = RPC; // you can specify whether to call the WebService envelope developed by DOTNET. DOTNET = true; // equivalent to envelope. bodyout = RPC; envelope. setoutputsoapobject (RPC); httptransportse transport = new httptransportse (endpoint); try {// call WebService transport. call (soapaction, envelope);} catch (exception e) {e. printstacktrace ();} // obtain the returned data soapobject object = (soapobject) envelope. bodyin; // obtain the returned result string result = object. getproperty ("getmobilecodeinforesult "). tostring (); return result ;}}