Web Services are online application Services released by enterprises to meet their specific business needs. Other companies or application software can access and use this online service over the Internet. Here, we use the soap protocol to send information to webservice, and then obtain the information returned by the webservice server to query the location information of the mobile phone number.
Web Services has many service providers. Here we use www.webxml.com.cn,
Click in to view the content defined by the soap protocol.
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 = St ReamTool. 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. setConnectTim Eout (5*1000); 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 pa Rser = 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) ;}is. close (); return OS. toByteArray ();}}