zoukankan      html  css  js  c++  java
  • Android实现号码归属地查询

    我们通过发送XML访问 WebService就可以实现号码的归属地查询,我们可以使用代理服务器提供的XML的格式进行设置,然后请求提交给服务器,服务器根据请求就会返回给一个XML,XML中就封装了我们想要获取的数据。

    发送XML

    1.通过URL封装路径打开一个HttpURLConnection

    2.设置请求方式,Content-Type和Content-Length

       XML文件的Content-Type为:application/soap+xml; charset=utf-8

    3.使用HttpURLConnection获取输出流输出数据

     

    WebService

    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中提供了电话归属地查询的使用说明

    效果图:

    示例代码:

    1. public class XmlService {  
    2.     public String query(String num) throws Exception {  
    3.         InputStream in = this.getClass().getClassLoader().getResourceAsStream("query.xml");  
    4.         byte[] data = LoadUtils.load(in);  
    5.         String xml = new String(data);  
    6.         //替换  
    7.         xmlxml = xml.replace("#", num);  
    8.         byte[] sendData = xml.getBytes("UTF-8");  
    9.         //发送到代理的地址上  
    10.         URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");  
    11.         HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
    12.         conn.setRequestMethod("POST");  
    13.         conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");  
    14.         conn.setRequestProperty("Content-Length", String.valueOf(sendData.length));  
    15.         //将请求的xml发送出去  
    16.         conn.setDoOutput(true);  
    17.         conn.getOutputStream().write(sendData);  
    18.   
    19.         //获取从服务器传回来的数据  
    20.         if (conn.getResponseCode() == 200)  
    21.             return parse(conn.getInputStream());  
    22.   
    23.         return null;  
    24.     }  
    25.   
    26.     //解析流拿到getMobileCodeInfoResult中的数据  
    27.     private String parse(InputStream inputStream) throws Exception {  
    28.         XmlPullParser parser = Xml.newPullParser();  
    29.         parser.setInput(inputStream, "UTF-8");  
    30.         //查找getMobileCodeInfoResult标签,获取标签中的数据  
    31.         for (int event = parser.getEventType(); event != XmlPullParser.END_DOCUMENT; event = parser.next())  
    32.             switch (event) {  
    33.                 case XmlPullParser.START_TAG:  
    34.                     if ("getMobileCodeInfoResult".equals(parser.getName()))  
    35.                         return parser.nextText();  
    36.             }  
    37.         return null;  
    38.     }  
    39. }  
  • 相关阅读:
    图,深度,广度优先遍历(一)
    java实现快速排序
    图,深度,广度优先遍历(二)
    图,深度,广度优先遍历(三)
    Jpa动态多表if多条件联合查询(if中包含list不为null和“=”的判断),并对查询结果进行分页
    SpringBoo启动报错:Failed to load property source from location ‘classpath:/bootstrap.yml‘
    Java对象创建和Javabean创建
    Linux解压命令
    BDD测试利器:mocha+should.js
    《老码识途》读书笔记:第一章(中)
  • 原文地址:https://www.cnblogs.com/rmbteam/p/2221772.html
Copyright © 2011-2022 走看看