CODY

CODY 关注TA

一坑未平,一坑起

CODY

CODY

关注TA

一坑未平,一坑起

  • 加入社区2,878天
  • 写了59,448字

该文章投稿至Nemo社区   Java  板块 复制链接


根据手机号码查询归属

发布于 2018/02/08 11:47 1,663浏览 2回复 4,507

方式一:使用google 手机号码校验库libphonenumber

依赖

 <dependency>  
    <groupId>com.googlecode.libphonenumber</groupId>  
    <artifactId>geocoder</artifactId>  
    <version>2.82</version>  
 </dependency> 
 <dependency>  
    <groupId>com.googlecode.libphonenumber</groupId>  
    <artifactId>libphonenumber</artifactId>  
    <version>8.8.5</version>  
 </dependency>  
 <dependency>  
    <groupId>com.googlecode.libphonenumber</groupId>  
    <artifactId>prefixmapper</artifactId>  
    <version>2.82</version>  
 </dependency>  
 <dependency>  
    <groupId>com.googlecode.libphonenumber</groupId>  
    <artifactId>carrier</artifactId>  
    <version>1.72</version>  
 </dependency>

代码

    @Test
    public void demo3(){
        String phone = "15899250569";   //手机号码
        int CountryCode = 86;            //国家编号
        Phonenumber.PhoneNumber pn = new Phonenumber.PhoneNumber();
        pn.setCountryCode(CountryCode);
        pn.setNationalNumber(Long.parseLong(phone));
        PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();
        String city = geocoder.getDescriptionForNumber(pn, Locale.CHINESE);
        System.err.println(city);
    }

结果

新疆乌鲁木齐市


方式二:使用webservice

依赖

      
            <groupId>org.apache.cxfgroupId>
            cxf-spring-boot-starter-jaxws</artifactId>
            3.1.12version>
        dependency>

代码

  public static void main(String[] args){
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl");
        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,PASS_WORD));
        Object[] objects = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke("getMobileCodeInfo", "13202281052", null);
            System.err.println("归属地: " + objects[0]);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
    }

结果

归属地: 13202281052:广东 深圳 广东联通GSM卡


方式三:使用阿里服务

参考 https://www.aliyun.com/ss/5omL5py65b2S5bGe5Zyw5YWN6LS5YXBp 

    @Test
    public void demo(){        
        String mobile = "13202214120";
        String url = "http://api04.aliyun.venuscn.com/mobile?mobile="+mobile;
        String appcode = "你自己的AppCode";
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add("Authorization", "APPCODE "+ appcode);        RestTemplate template = new RestTemplate();
        HttpEntity requestEntity = new HttpEntity(null, requestHeaders);
        ResponseEntity response = template.exchange(url, HttpMethod.GET, requestEntity, String.class);
        System.err.println(response.getBody());
    }

结果

{"ret":200,"msg":"success","data":{"types":"中国联通 GSM","lng":"116.372831","city":"揭阳","num":1320221,"isp":"联通","area_code":"445200","city_code":"0663","prov":"广东","zip_code":"522000","lat":"23.549993"},"log_id":"FFBDF8E5-6190-47BB-B976-ACE7E379F330"}

方式四:本地创建手机号码库,进行匹配

CREATE TABLE `mobile_ascription` (
  `id` bigint(15) NOT NULL AUTO_INCREMENT primary key,
  `mobile` varchar(7) default NULL COMMENT '手机号码',
  `province` varchar(3) default NULL COMMENT '省份',
  `city` varchar(5) default NULL COMMENT '城市',
  `corp` varchar(4) default NULL COMMENT '运营商',
  `area_code` varchar(4) default NULL COMMENT '区号',
  `post_code` varchar(6) default NULL COMMENT '邮政编码',
  `created_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `created_by` varchar(30) DEFAULT NULL COMMENT '创建用户',
  `updated_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  `updated_by` varchar(30) DEFAULT NULL COMMENT '修改用户'
) COMMENT='手机归属' AUTO_INCREMENT=10000 ;

-- 新增索引
create index ix_mascription_mobile on mobile_ascription(mobile);

数据

参考:https://gitee.com/codygit/cody-mobile_ascription/blob/master/data.7z 

本文标签
 {{tag}}
点了个评