Public class addressactivity extends activity {/** called when the activity is first created. */private button; private edittext; private textview; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); edittext = (edittext) findviewbyid (R. id. phone); button = (button) findviewbyid (R. id. button); textview = (textview) findviewbyid (R. id. result); button. setonclicklistener (New onclicklistener () {public void onclick (view v) {// todo auto-generated method stubstring mobile = edittext. gettext (). tostring (); try {string S = new mobilehttpservice (addressactivity. this. getresources (). getassets (). open ("soap. XML ")). httppost (mobile); textview. settext (s);} catch (exception e) {toast. maketext (addressactivity. this, "failed to get", 0 );}}});}}
Public class mobilehttpservice {private inputstream soapinput; Public mobilehttpservice (inputstream is) {This. soapinput = is;} // access address private string Path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"; // get the Public String httppost (string mobile) throws exception {string strsoap = readsoap (); strsoap = strsoap. replaceall ("\ $ mobile", mobile); byte [] DATA = strsoap. getbytes (); Url url = new URL (PATH); httpurlconnection conn = (httpurlconnection) URL. openconnection (); Conn. setconnecttimeout (5000); Conn. setrequestmethod ("Post"); // use the POST method Conn. setdooutput (true); // you must specify conn to write data. setchunkedstreamingmode (0); // recommended in this document // set the request header parameter Conn. setrequestproperty ("Content-Type", "application/soap + XML; charset = UTF-8"); Conn. setrequestproperty ("Content-Length", String. valueof (data. length); Conn. getoutputstream (). write (data); If (Conn. getresponsecode () = 200) {return parsersoap (Conn. getinputstream ();} return NULL;}/*** parse the returned soap data ** @ Param inputstream * @ throws exception */private string parsersoap (inputstream is) throws exception {string S = util. getstring (is); log. I ("result", S); Return s;}/*** get soap file data ** @ return * @ throws exception */private string readsoap () throws exception {string STR = util. getstring (this. soapinput); Return STR ;}}
Public class util {/*** read character data from the input stream ** @ Param is * @ return * @ throws exception */public static string getstring (inputstream is) throws exception {stringbuffer stringbuff = new stringbuffer (); byte [] B = new byte [1024]; int Len = 0; while (-1! = (LEN = is. read (B, 0,200) {string TMP = new string (B, 0, Len, "UTF-8"); stringbuff. append (TMP);} return stringbuff. tostring ();}}
Modified
Parse XML