查询手机归属地API集合 :
(淘宝只能查省)https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=*****填写手机号*****
(百付宝只能查省)https://www.baifubao.com/callback?cmd=1059&callback=phone&phone=*****填写手机号*****
(百度获取省市)http://apistore.baidu.com/apiworks/servicedetail/794.html
这里以百度的为例:
/**
* 根据传进来的手机号获取到归属地
*/
public String getMobilAddress(String mobile){
String httpURL = "http://apis.baidu.com/apistore/mobilenumber/mobilenumber";
String httpArg = "phone="+mobile;
String jsonResult = null;
BufferedReader br = null;
HttpURLConnection conn = null;
StringBuffer sb = new StringBuffer();
try{
URL url = new URL(httpURL+"?"+httpArg);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("apikey", "2e83cded7ab1e276dac40a0c8fd98c3d");//这里是个人的百度的apikey,请前往百度个人中心获取
conn.connect();
br = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
String strRead = null;
while((strRead = br.readLine())!=null){
sb.append(strRead);
sb.append("\r\n");
}
br.close();
jsonResult = sb.toString();
if(StringUtils.isNotEmpty(jsonResult)){
JSONObject json = JSONObject.parseObject(jsonResult);
if("0".equals(json.get("errNum").toString())){
StringBuffer sbf = new StringBuffer();
JSONObject optJson = json.getJSONObject("retData");
sbf.append(optJson.getString("province"))//获取省
.append(optJson.getString("supplier"));//获取通信的公司(移动电信联通)
return sbf.toString();
}
}
}catch(Exception e){
LOG.debug("getMobilAddress error. e{}",e);
}finally{
conn.disconnect();
}
return null;
}
这里我返回的字符串时省+通信公司,要想获取具体的信息,请参考API接口返回的json格式:
JSON返回示例 :
{
"errNum": 0,
"retMsg": "success",
"retData": {
"phone": "15210011578",
"prefix": "1521001",
"supplier": "移动 ",
"province": "北京 ",
"city": "北京 ",
"suit": "152卡"
}
}
备注 :
"phone": 手机号码,
"prefix": 手机号码前7位,
"supplier": "移动 ",
"province": 省份,
"city": 城市,
"suit": "152卡"