调用webservice获取电话号码归属地信息

首先什么是webservice ?

从广义上面讲,任何一个服务器所提供的"数据","内容","方法"等等都可以理解为webservice. 本例是一个狭义的webservice
本例所使用的webservice地址:http://www.webxml.com.cn/zh_cn/index.aspx
 
1.打开这个网站,然后找到号码归属地
2.进入后会看到 getMobileCodeInfogetDatabaseInfo两种获取归属地的方式,前面的是基于SOAP(肥皂协议)协议的;后面的是基于数据库的
本例采用SOAP协议编写,点击 getMobileCodeInfo进入相应页面,会看到SOAP1.1/1.2, 这里使用1.2的
SOAP 1.2
以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。

POST /WebServices/MobileCodeWS.asmx HTTP/1.1
Host: webservice.webxml.com.cn
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <getMobileCodeInfo xmlns="http://WebXml.com.cn/">
      <mobileCode>string</mobileCode>
      <userID>string</userID>
    </getMobileCodeInfo>
  </soap12:Body>
</soap12:Envelope>
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">
      <getMobileCodeInfoResult>string</getMobileCodeInfoResult>
    </getMobileCodeInfoResponse>
  </soap12:Body>
</soap12:Envelope>

根据上面给出的xml得知,第一个xml文件是post要提交的数据,根据提示请求的数据需要用占位符
<mobileCode>$mobile</mobileCode>  这里给号码加入占位符
<userID></userID>          这个号码所对应的唯一编号,可以留空,因为不处理它

3.把post请求的xml文件拷贝到项目中,这里是拷贝到了src下面--post.xml

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <getMobileCodeInfo xmlns="http://WebXml.com.cn/">
      <mobileCode>$mobile</mobileCode>
      <userID></userID>
    </getMobileCodeInfo>
  </soap12:Body>
</soap12:Envelope>

具体代码如下NumberService类--处理post请求以及获取服务器返回的归属地信息

public class NumberService {
    /**
     * 查询号码归属地
     * 
     * @param number
     * @return
     * @throws Exception
     */
    public static String getAddress(String number) throws Exception {
        // 把soap肥皂协议-以post方式提交的xml数据获取出来
        InputStream is = NumberService.class.getClassLoader()
                .getResourceAsStream("data.xml");
        byte[] xml = StreamTool.getBytes(is);
        String postxml = new String(xml);
        //获取将要提交的xml中的 手机号,$mobile为号码占位符  --- 这个xml就相当于 一个jsp表单
        String phoneNumber = postxml.replace("$mobile", number);
        //以post方式发送数据给服务器
        String numberAddress = sendDataByPost(phoneNumber);
        return numberAddress;
    }

    /**
     * 以post方式请求服务器
     * 
     * @param result
     *            参数就是要查询的手机号码
     * @return
     * @throws Exception
     */
    public static String sendDataByPost(String phoneNumber) throws Exception {
        URL url = new URL(
                "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");
        String data = phoneNumber; // 要传递的数据
        //byte[] bytes = data.getBytes();

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        //设置超时连接
        conn.setReadTimeout(5000);
        //设置请求方式
        conn.setRequestMethod("POST");
        //设置post请求的参数
        conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
        conn.setRequestProperty("Content-Length", data.length()+"");
        
        //允许http协议对外输出信息
        conn.setDoOutput(true);
        //把数据写给服务器
        conn.getOutputStream().write(data.getBytes());

        int code = conn.getResponseCode();
        if(code==200){
            //获取从服务器中返回过来的流(由于返回的数据是xml格式,所以需要解析xml)
            InputStream is = conn.getInputStream();
            String address = ResultXml.getAddress(is);
            return address;
        }else{
            return "访问网络出错";
        }
    }
}

根据上面服务器给出的xml相应文件--需要解析getMobileCodeInfoResult就可以获取手机对属地信息
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">
      <getMobileCodeInfoResult>string</getMobileCodeInfoResult>
    </getMobileCodeInfoResponse>
  </soap12:Body>
</soap12:Envelope>

解析类如下

/**
 * 解析服务器返回的xml数据,获取归属地
 * @author Administrator
 *
 */
public class ResultXml {
    public static String getAddress(InputStream is) throws Exception{
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(is, "utf-8");
        int type = parser.getEventType();
        
        while(type!=XmlPullParser.END_DOCUMENT){
            if(type==XmlPullParser.START_TAG){
                if("getMobileCodeInfoResult".equals(parser.getName())){
                    return parser.nextText();
                }
            }
            type = parser.next();
        }
        return "没有该号码的信息";
    }
}

上面代码中使用了一个工具类StreamTool

public class StreamTool {
    /**
     * 
     * @param is 参数InputStream代表从服务器返回过来的流(流中存储了我们所需的数据)
     * @return
     * @throws Exception
     * 这里把服务器流中的数据循环的写入到了内存数组流ByteArrayOutputStream里面
     * 作用是内存操作数据快.最后以字节数组返回
     */
    public static byte[] getBytes(InputStream is) throws Exception{
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while((len = is.read(buffer))!=-1){
            bos.write(buffer, 0, len);
        }
        is.close();
        bos.flush();
        byte[] result = bos.toByteArray();
        System.out.println(new String(result));
        return  result;
    }
}

测试截图

 

 

转载于:https://www.cnblogs.com/android-zcq/p/3388322.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值