咱们经过发送XML访问 WebService就能够实现号码的归属地查询,咱们可使用代理服务器提供的XML的格式进行设置,而后请求提交给服务器,服务器根据请求就会返回给一个XML,XML中就封装了咱们想要获取的数据。 html
http://www.baisoujs.com web
发送XML 服务器
http://www.baisoujs.com/detail_137571041148792.html 网络
1.经过URL封装路径打开一个HttpURLConnection app
2.设置请求方式,Content-Type和Content-Length 网站
XML文件的Content-Type为:application/soap+xml; charset=utf-8 this
3.使用HttpURLConnection获取输出流输出数据 url
WebService spa
1.WebService是发布在网络上的API,能够经过发送XML调用,WebService返回结果也是XML数据 代理
2.WebService没有语言限制,只要能够发送XML数据和接收XML数据便可
3.http://www.webxml.com.cn/网站上提供了一些WebService服务,咱们能够对其进行调用
4.http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo中提供了电话归属地查询的使用说明
效果图:
示例代码:
- public class XmlService {
- public String query(String num) throws Exception {
- InputStream in = this.getClass().getClassLoader().getResourceAsStream("query.xml");
- byte[] data = LoadUtils.load(in);
- String xml = new String(data);
- //替换
- xmlxml = xml.replace("#", num);
- byte[] sendData = xml.getBytes("UTF-8");
- //发送到代理的地址上
- URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("POST");
- conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
- conn.setRequestProperty("Content-Length", String.valueOf(sendData.length));
- //将请求的xml发送出去
- conn.setDoOutput(true);
- conn.getOutputStream().write(sendData);
-
- //获取从服务器传回来的数据
- if (conn.getResponseCode() == 200)
- return parse(conn.getInputStream());
-
- return null;
- }
-
- //解析流拿到getMobileCodeInfoResult中的数据
- private String parse(InputStream inputStream) throws Exception {
- XmlPullParser parser = Xml.newPullParser();
- parser.setInput(inputStream, "UTF-8");
- //查找getMobileCodeInfoResult标签,获取标签中的数据
- for (int event = parser.getEventType(); event != XmlPullParser.END_DOCUMENT; event = parser.next())
- switch (event) {
- case XmlPullParser.START_TAG:
- if ("getMobileCodeInfoResult".equals(parser.getName()))
- return parser.nextText();
- }
- return null;
- }
- }
http://www.baisoujs.com/detail_137571041148792.html