Android obtains the home location of the mobile phone

Source: Internet
Author: User
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

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.