WEB Services is an online application service published by an enterprise that accomplishes its specific business needs, and other companies or applications can access and use the online service over the Internet. Here we use the SOAP protocol to send information to the webservice, and then get the information returned by the WebService server, in order to query the cell phone number attribution information.
WEB Services has a lot of service providers, where we use www.webxml.com.cn,
Click to go in, we can see the contents of the SOAP protocol definition.
The next step is to write the code.
Package Cn.mzba.service; import Java.io.inputstream;import Java.io.outputstream;import Java.net.httpurlconnection;import Java.net.url;import Org.xmlpull.v1.xmlpullparser;import android.util.Xml; public class Mobileservice { public static string findaddress (String mobile) throws Exception { inputstream is = MobileService.class.getClassLoader () . getResourceAsStream ("Mobilesoap.xml"); byte[] data = Streamtool.readstream (IS); string xml = new String (data, "UTF-8"); string soap = Xml.replaceall ("\ \ $mobile", mobile); byte[] result = Soap.getbytes ("UTF-8"); string Path = "Http://webseRvice.webxml.com.cn/webservices/mobilecodews.asmx "; url URL = new URL (path); httpurlconnection conn = (httpurlconnection) Url.openconnection (); conn.setrequestmethod ("POST"); conn.setdooutput (True); conn.setconnecttimeout (5 *); Conn.setrequestproperty ("Content-type", "Application/soap+xml; Charset=utf-8 "); conn.setrequestproperty (" Content-Length ", String.valueof (Result.length)); outputstream OS = Conn.getoutputstream (); os.write (Result); os.flush (); os.close (); inputstream ISSOCP = Conn.getinputstream (); return Parse (ISSOCP); } public static String Parse (InputStream is) throws Exception { xmlpullparser parser = Xml.newpullparser (); parser.setinput (IS, "UTF-8"); int event = Parser.geteventtype (); while (Event! = xmlpullparser.end_document) { switch (event) { case xmlpullparser.start_tag: if ("Getmobilecodeinforesult". Equals (Parser.getname ())) { return Parser.nexttext (); } break; } event = Parser.next (); } return null; }}
Package Cn.mzba.service; import Java.io.bytearrayoutputstream;import Java.io.inputstream; public class Streamtool { /** * read input stream data * * @param is * @return * @throws exception */ public Static byte[] Readstream (InputStream is) throws Exception { bytearrayoutputstream OS = new Bytearrayoutputstream (); byte[] buffer = new byte[2048]; int len = 0; while (len = is.read (buffer))! =-1) { os.write (buffer, 0, Len); } &nbsP; is.close (); return Os.tobytearray (); }}