首先我们可以进入webservice的网站 http://www.webxml.com.cn/zh_cn/index.aspx
然后找到手机服务:
我们可以先测试一下:
我们随便输入一个号码:
这是别人提供给我们的服务,我们可以调用并得到相应的数据
首先我们点击进入服务说明
生成对应的WSDL网址: http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL
我们可以使用jdk自带的工具将其解析成java文件
wsimport -s ./ -p cn.qblank.wsimport http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL
命令 解析的路径 包名 url
然后我们就会在解析的路径下看到对应的java文件:
我们可以将这些文件拷到项目中:
接下来我们就来获取下数据:
package cn.qblank.webservice101;
import cn.qblank.wsimport.MobileCodeWS;
import cn.qblank.wsimport.MobileCodeWSSoap;
public class Test {
public static void main(String[] args) {
//生成服务对象
MobileCodeWS ws = new MobileCodeWS();
//取得webservice服务的访问方式 : Soap1.1 Soap 1.2 Http-get http-Post
MobileCodeWSSoap mobileCodeWSSoap = ws.getMobileCodeWSSoap();
/**
* 返回的数据有两种类型 :
* 1. 简单的数据类型 。基本数据类型 :整数、布尔、字符串 等
* 2. 复合的数据类型 :结构体 ,数组 ,对象
*/
//简单的数据类型:
String message = mobileCodeWSSoap.getMobileCodeInfo("18957453853", "");
//截取手机号
String phone = message.substring(0, message.indexOf(":"));
//截取地址
String address = message.substring(message.indexOf(":") + 1);
//获取省份
String[] strs = address.split(" ");
//获取省份
String province = strs[0];
//获取城市
String city = strs[1];
//获取卡的类型
String cardType = strs[2];
System.out.println("手机号:" + phone);
System.out.println("省份:" + province + "\n城市:" + city + "\n卡的类型:" + cardType);
}
}
获取的数据如下: