在java中如何根据手机号查询号码归属地
1、maven项目中配置
<dependency>
<groupId>com.googlecode.libphonenumber</groupId>
<artifactId>geocoder</artifactId>
<version>2.15</version>
</dependency>
<dependency>
<groupId>com.googlecode.libphonenumber</groupId>
<artifactId>libphonenumber</artifactId>
<version>6.3</version>
</dependency>
<dependency>
<groupId>com.googlecode.libphonenumber</groupId>
<artifactId>prefixmapper</artifactId>
<version>2.15</version>
</dependency>
<dependency>
<groupId>com.googlecode.libphonenumber</groupId>
<artifactId>carrier</artifactId>
<version>1.5</version>
</dependency>
2、项目中如何调用查询接口:
private PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
PhoneNumberOfflineGeocoder phoneNumberOfflineGeocoder = PhoneNumberOfflineGeocoder.getInstance();
String language =”CN”;
PhoneNumber referencePhonenumber = null;
String phoneNum = “1872171XXXX”;
try {
referencePhonenumber = phoneUtil.parse(phoneNum, language);
} catch (NumberParseException e) {
e.printStackTrace();
}
//手机号码归属城市 city
String city= phoneNumberOfflineGeocoder.getDescriptionForNumber(referencePhonenumber,Locale.CHINA);
这样就能很简单的去查询号码归属地了,而不用去请求其他第三方的接口了。