java web免费手机号码归属地接口
java类 PhoneAPI
package com.webber.cm.outsideapi.baidu;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;
import com.webber.cm.common.util.HttpClient;
import com.webber.cm.common.util.JsonUtil;
// 电话
public class PhoneAPI {
static Logger logger = Logger.getLogger(PhoneAPI.class);
public static void main(String[] args) {
String phone = PhoneAPI.phoneLocation("15788888888");
System.out.println(phone);
}
// 手机号归属地
public static String phoneLocation(String phone) {
if(!PhoneAPI.isMobile(phone)) {
return "号码错误";
}
String result = null;
try {
String url = "http://mobsec-dianhua.baidu.com/dianhua_api/open/location?tel=PHONE";
url = url.replace("PHONE", phone);
String reqResult = HttpClient.doGet(url);
if(reqResult.indexOf("null")>-1) {
return "号码有误";
}
Map<String, Object> map = JsonUtil.parseJSON2Map(reqResult);
String location = ((Map) ((Map) map.get("response")).get(phone)).get("location").toString();
result = location.substring(0, location.length() - 2);
} catch (Exception e) {
logger.error("手机归属接口异常:", e);
}
logger.info("手机归属接口:{phone:" + phone + ",result:" + result + "}");
return result;
}
// 手机号码格式检查
private static Boolean isMobile(String phone) {
String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$";
if (phone.length() != 11) {
return false;
} else {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(phone);
boolean isMatch = m.matches();
if (isMatch) {
return true;
} else {
return false;
}
}
}
}
完
希望以上代码能够帮助到你!