之前找过一些查询手机号的API不是归属地返回不全,就是需要收费
现在找到一个用起来还不错的API,分享一下。
public void findAddr(String phone) {
String findUrl = "http://mobsec-dianhua.baidu.com/dianhua_api/open/location";
//正则验证手机号 170/171疑似可以网络刷号故此限制
String regex = "^(1[345689]\\d{9})|((17[^01])\\d{8})$";
if (phone.length() != 11) {
throw new EcRuntimeException("手机号应为11位数");
} else {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(phone);
boolean isMatch = m.matches();
if (!isMatch) {
throw new EcRuntimeException("请填入正确的手机号");
}
try {
HttpResponse<String> jsonResponse = Unirest
.get(findUrl)
.queryString("tel", phone)
.asString();
if (jsonResponse.getStatus() == 200 && "OK".equals(jsonResponse.getStatusText())) {
System.out.println(jsonResponse.getBody());
}
} catch (Exception e) {
e.printStackTrace();
}
}
//测试main方法
public static void main(String[] args) {
Test test=new Test ();
test.findAddr("手机号");
}
返回结果如下,大家取需要的值即可:
{"response":{"134********":{"detail":{"area":[{"city":"烟台"}],"province":"山东","type":"domestic","operator":"移动"},"location":"山东烟台移动"}},"responseHeader":{"status":200,"time":1547016158837,"version":"1.1.0"}}