我正在使用libphonenumber jar(版本7)并尝试解析电话号码而不设置Region。我按照推荐看到了以下代码。
PhoneNumber phoneNumber = phoneUtil.parse(numberStr, ""));
但是,这会抛出INVALID_COUNTRY_CODE异常。最新版本有什么变化吗?如何获得给定数字的区域代码(US),如(“+ 1-xxx-xxx-xxxx”)?
答案 0 :(得分:0)
I used libphonenumber 7.0.8 and it worked:
public static void main(final String[] args) {
String numberStr = "+1 650-253-0000"; // Google
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();
try {
PhoneNumber phoneNumber = phoneUtil.parse(numberStr, null);
System.out.println(geocoder.getDescriptionForNumber(phoneNumber, Locale.ENGLISH));
} catch (NumberParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
It worked using:
phoneUtil.parse(numberStr, null);
phoneUtil.parse(numberStr, "");
phoneUtil.parse(numberStr, "ZZ");
Output is always Mountain View, CA
- so the number was parsed successfully.
From the javadoc you can find this information:
If the number is guaranteed to start with a '+' followed by the country calling code, then "ZZ" or null can be supplied.