Call webservice to query the mobile phone number attribution Information

Source: Internet
Author: User

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 ();}}

Related Article

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.