Package com. example. usewebservice; import org. ksoap2.SoapEnvelope; import org. ksoap2.serialization. soapObject; import org. ksoap2.serialization. soapSerializationEnvelope; import org. ksoap2.transport. httpTransportSE; import android. OS. bundle; import android. OS. handler; import android. OS. logoff; import android. OS. message; import android. annotation. suppressLint; import android. app. activity; import android. view. me Nu; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. editText; import android. widget. textView; import android. widget. toast; public class MainActivity extends Activity {/***** Android platform calls WebService (mobile phone number attribution query) * @ author yejianping * @ date 2014-4-3 * first import the library of the ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar to lib ***/public EditTe Xt text; public Button button; public TextView tx; public String telephone_number; public MyThread thread; public Handler handler; @ SuppressLint ("HandlerLeak") @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); text = (EditText) findViewById (R. id. editText1); button = (Button) findViewById (R. id. button1); thread = new MyThread (); Button. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// method stub automatically generated by TODO: telephone_number = text. getText (). toString (). trim (); if (telephone_number.equals ("") | telephone_number.length () <7) {text. setError ("the phone number (segment) You entered is incorrect! "); Text. requestFocus ();} else {new Thread (thread ). start () ;}}); handler = new Handler () {@ SuppressLint ("HandlerLeak") public void handleMessage (Message msg) {switch (msg. what) {case 0x01: Bundle bundle = new Bundle (); bundle = msg. getData (); Toast. makeText (MainActivity. this, bundle. getString ("result"), Toast. LENGTH_SHORT ). show () ;}};}// create the public class MyThread implements Runnable {public void run () {loid. prepare (); // create the message queue of this thread and initialize getTelephoneInfo (telephone_number); logoff. loop (); // start running Message Queue} public void getTelephoneInfo (String phone_number) {// nameSpace String nameSpace = "http://WebXml.com.cn /"; // call the method name String methodName = "getMobileCodeInfo"; // webservice URL String URL = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"; // namespace + method String soapAction = "http://WebXml.com.cn/getMobileCodeInfo "; // specify the WebService nameSpace and method name SoapObject rpc = new SoapObject (nameSpace, methodName); // set the two parameters mobileCode and userIdrpc to be passed in to call the WebService interface. addProperty ("mobileCode", phone_number); 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; // set whether to call WebServiceenvelope developed by dotNet. dotNet = true; // equivalent to envelope. bodyOut = rpc; envelope. setOutputSoapObject (rpc); HttpTransportSE transport = new HttpTransportSE (URL); try {// call WebServicetransport. 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 (); Message msg = new Message (); Bundle bundle = new Bundle (); bundle. putString ("result", result); msg. setData (bundle); msg. what = 0x01; handler. handleMessage (msg); // return result; // display the results returned by WebService in TextView. // tx. setText (result) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
The xml code is;
Source code download