java通过手机号查询地区信息

通过手机号查询省份城市。

有两种方式:

一,通过发送url请求获取信息。此方法在遍历手机号查询时效率很低。

private String getProvinceName(String phoneNum){
        //淘宝网的API地址
        String urlString = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel="
                + phoneNum;

        StringBuffer sb = new StringBuffer();
        BufferedReader buffer;
        URL url = null;
        try {
            url = new URL(urlString);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        String province = "";
        try {
            //获取URL地址中的页面内容
            InputStream in = url.openStream();
            // 解决乱码问题
            buffer = new BufferedReader(new InputStreamReader(in, "gb2312"));
            String line = null;
            //一行一行的读取数据
            while ((line = buffer.readLine()) != null) {
                sb.append(line);
            }
            in.close();
            buffer.close();
            String retJson = sb.toString();

           if(retJson!= null && retJson.length() > 0 && retJson.indexOf("=") != -1){
               retJson = retJson.substring(retJson.indexOf("=")+1);
                province = JSONObject.parseObject(retJson).getString("province");
           }

        } catch (Exception e) {
            e.printStackTrace();
        }
        //从JSONObject对象中读取城市名称
        return province;

二,通过工具类查询。

在pom文件中导入依赖

        
        
            com.googlecode.libphonenumber
            geocoder
            2.15
        

        
            com.googlecode.libphonenumber
            libphonenumber
            6.3
        

        
            com.googlecode.libphonenumber
            prefixmapper
            2.15
        
        
            com.googlecode.libphonenumber
            carrier
            1.5
        
        

编写返回的model

public class PhoneModel {
    /** 省份名称 */
    private String provinceName;

    /** 城市名称 */
    private String cityName;

    /** 运营商:移动/电信/联通 */
    private String carrier;

    public String getProvinceName() {
        return provinceName;
    }

    public void setProvinceName(String provinceName) {
        this.provinceName = provinceName;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getCarrier() {
        return carrier;
    }

    public void setCarrier(String carrier) {
        this.carrier = carrier;
    }
}

 

编写工具类

import com.google.i18n.phonenumbers.PhoneNumberToCarrierMapper;
import com.google.i18n.phonenumbers.Phonenumber;
import com.google.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder;
import com.xhks.dto.PhoneModel;

import java.util.Locale;

public class PhoneNumberUtil {
    /** 直辖市 */
    private final static String[] MUNICIPALITY = { "北京市", "天津市", "上海市", "重庆市" };

    /** 自治区 */
    private final static String[] AUTONOMOUS_REGION = { "新疆", "内蒙古", "西藏",
            "宁夏", "广西" };

    private static com.google.i18n.phonenumbers.PhoneNumberUtil phoneNumberUtil = com.google.i18n.phonenumbers.PhoneNumberUtil
            .getInstance();

    /** 提供与电话号码相关的运营商信息 */
    private static PhoneNumberToCarrierMapper carrierMapper = PhoneNumberToCarrierMapper
            .getInstance();

    /** 提供与电话号码有关的地理信息 */
    private static PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder
            .getInstance();

    /** 中国大陆区区号 */
    private final static int COUNTRY_CODE = 86;

    /**
     * 根据手机号 判断手机号是否有效
     *
     * @param phoneNumber
     *            手机号码
     * @return true-有效 false-无效
     */
    public static boolean checkPhoneNumber(String phoneNumber) {
        long phone = Long.parseLong(phoneNumber);

        Phonenumber.PhoneNumber pn = new Phonenumber.PhoneNumber();
        pn.setCountryCode(COUNTRY_CODE);
        pn.setNationalNumber(phone);

        return phoneNumberUtil.isValidNumber(pn);

    }

    /**
     * 根据手机号 判断手机运营商
     *
     * @param phoneNumber
     *            手机号码
     * @return 如:广东省广州市移动
     */
    public static String getCarrier(String phoneNumber) {

        long phone = Long.parseLong(phoneNumber);

        Phonenumber.PhoneNumber pn = new Phonenumber.PhoneNumber();
        pn.setCountryCode(COUNTRY_CODE);
        pn.setNationalNumber(phone);
        // 返回结果只有英文,自己转成成中文
        String carrierEn = carrierMapper.getNameForNumber(pn, Locale.ENGLISH);
        String carrierZh = "";
        switch (carrierEn) {
            case "China Mobile":
                carrierZh += "移动";
                break;
            case "China Unicom":
                carrierZh += "联通";
                break;
            case "China Telecom":
                carrierZh += "电信";
                break;
            default:
                break;
        }
        return carrierZh;
    }

    /**
     * 根据手机号 获取手机归属地
     *
     * @param phoneNumber
     *            手机号码
     * @return 如:广东省广州市
     */
    public static String getGeo(String phoneNumber) {
        long phone = Long.parseLong(phoneNumber);

        Phonenumber.PhoneNumber pn = new Phonenumber.PhoneNumber();
        pn.setCountryCode(COUNTRY_CODE);
        pn.setNationalNumber(phone);
        return geocoder.getDescriptionForNumber(pn, Locale.CHINESE);
    }

    /**
     * 根据手机号 获取手机信息模型
     *
     * 
     * 若返回值为null,则说明该号码无效
     * 
* * @param phoneNumber * 手机号码 * @return 手机信息模型PhoneModel */ public static PhoneModel getPhoneModel(String phoneNumber) { if (checkPhoneNumber(phoneNumber)) { String geo = getGeo(phoneNumber); PhoneModel phoneModel = new PhoneModel(); String carrier = getCarrier(phoneNumber); phoneModel.setCarrier(carrier); // 直辖市 for (String val : MUNICIPALITY) { if (geo.equals(val)) { phoneModel.setProvinceName(val.replace("市", "")); phoneModel.setCityName(val); return phoneModel; } } // 自治区 for (String val : AUTONOMOUS_REGION) { if (geo.startsWith(val)) { phoneModel.setProvinceName(val); phoneModel.setCityName(geo.replace(val, "")); return phoneModel; } } // 其它 String[] splitArr = geo.split("省"); if (splitArr != null && splitArr.length == 2) { phoneModel.setProvinceName(splitArr[0]); phoneModel.setCityName(splitArr[1]); return phoneModel; } } return null; } }

调用工具类的getPhoneModel方法回去model 

 PhoneModel phoneModel = PhoneNumberUtil.getPhoneModel(callLogDtos.get(i).getPhoneNumber());

入参:手机号;

出参:PhoneModel。

通过PhoneModel获取省份,城市。

 

你可能感兴趣的:(java技术)