利用百度Api查询手机号归属地
先到百度API官网查看其有关说明,这是其官网:http://apistore.baidu.com/
可在此页面找到相关说明。
打开网址可以看到好几个号码归属地的查询,有些是收费的,有些免费,如果调用不是特别频繁,需要的信息量不是特别多的话,免费的就足够了。我开始看的是第一个接口:http://apistore.baidu.com/apiworks/servicedetail/117.html
但是百度自己的例子中就可以看出这个接口并不符合我的需求,我要的是可以查询到省市的,显然这个接口只返回给我省。
再来看这个 http://apistore.baidu.com/apiworks/servicedetail/794.html
首先看百度所给的例子的结果:
看来这个可以了,百度给了几种不同语言的例子,我只看java的:
String httpUrl = “http://apis.baidu.com/apistore/mobilenumber/mobilenumber“;
String httpArg = “phone=15210011578”;
String jsonResult = request(httpUrl, httpArg);
System.out.println(jsonResult);
/**
* @param urlAll
* :请求接口
* @param httpArg
* :参数
* @return 返回结果
*/
public static String request(String httpUrl, String httpArg) {
BufferedReader reader = null;
String result = null;
StringBuffer sbf = new StringBuffer();
httpUrl = httpUrl + “?” + httpArg;
try {
URL url = new URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod("GET");
// 填入apikey到HTTP header
connection.setRequestProperty("apikey", "您自己的apikey");
connection.connect();
InputStream is = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sbf.append(strRead);
sbf.append("\r\n");
}
reader.close();
result = sbf.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
这里边需要有自己的百度apikey 可参考这个网页http://app.baidu.com/static/cms/getapikey.html
可以把这个方法封装一下作为一个工具类使用了。
本文介绍了如何利用百度API查询手机号的归属地。首先访问百度API官网,比较了不同接口的功能,选择了能提供省市信息的接口http://apistore.baidu.com/apiworks/servicedetail/794.html。通过示例代码展示了使用Java调用API的过程,并提醒需要拥有自己的百度API key。
9885

被折叠的 条评论
为什么被折叠?



