java获取手机号归属地

本文介绍两种在Java中获取手机号归属地的方法:一是通过API请求,二是利用jar包。详细展示了如何使用PhoneNumberUtil和PhoneNumberOfflineGeocoder解析手机号并获取其归属城市,同时提供了一个从特定网站抓取归属地信息的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java获取手机号归属地

这里有两种方法,一种根据api请求,一种利用jar包获取,按需选其中一种,或两者结合使用
package com.jjj.util;

import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import com.google.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Locale;

/**
 * @创建人 sanbusong
 * @创建时间 2018/12/13
 * @描述
 */
public class CityUtil {
    public static String getCity(String phoneNum){
        PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
        PhoneNumberOfflineGeocoder phoneNumberOfflineGeocoder = PhoneNumberOfflineGeocoder.getInstance();
        String language ="CN";
        Phonenumber.PhoneNumber referencePhonenumber = null;
        try {
            referencePhonenumber = phoneUtil.parse(phoneNum, language);
        } catch (NumberParseException e) {
            e.printStackTrace();
        }
//手机号码归属城市 city
        String city= phoneNumberOfflineGeocoder.getDescriptionForNumber(referencePhonenumber,Locale.CHINA);
        return city;
    }

    /**
     * 从www.ip138.com返回的结果网页内容中获取手机号码归属地,结果为:省份 城市
     *	选用这个的原因。是这个在尝试的数个获取api里面是比较精确的。
     * @param htmlSource
     * @return
     */
    public static String getCityUrl(String mobile) {
        String url = "http://www.ip138.com:8080/search.asp";
        StringBuffer sb = new StringBuffer(url);
        sb.append("?mobile="+mobile);
        sb.append("&action=mobile");
        // 指定get请求
        HttpGet httpGet = new HttpGet(sb.toString());
        // 创建httpclient
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 发送请求
        HttpResponse httpResponse;
        //返回的json
        String result = null;
        try {
            httpResponse = httpClient.execute(httpGet);
            // 验证请求是否成功
            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                // 得到请求响应信息
                String str = EntityUtils.toString(httpResponse.getEntity(),
                        "GB2312");
                // 返回json
                if(str!=null&&!str.equals("")){
                    result=parseMobileFrom(str);
                }
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }


    public static String parseMobileFrom(String htmlSource){
        String result = "";
        String[] htmls = htmlSource.split("\n");
        for (int i = 0; i < htmls.length; i++) {
            String thisHtml = htmls[i];
            if(thisHtml.indexOf("卡号归属地")>0){
                if(thisHtml.indexOf("tdc2")>0){
                    thisHtml = thisHtml.substring(0,thisHtml.lastIndexOf("<"));
                    result = thisHtml.substring(thisHtml.lastIndexOf(">")+1);
                }else{
                    thisHtml = htmls[i+1];
                    thisHtml = thisHtml.substring(0,thisHtml.lastIndexOf("<"));
                    result = thisHtml.substring(thisHtml.lastIndexOf(">")+1);
                }
            }
        }
        return result.replaceAll("&nbsp;","");
    }

    public static void main(String[] args) {
        System.out.println("getCityUrl");
        System.out.println(getCityUrl("13569122222"));
        System.out.println("getCity");
        System.out.println(getCity("13569122222"));
    }
}

附pom;

<!--请求包-->
<dependency>
	<groupId>org.apache.httpcomponents</groupId>
	<artifactId>httpclient</artifactId>
	<version>4.5.3</version>
</dependency>
<!--城市获取-->
<dependency>
	<groupId>com.googlecode.libphonenumber</groupId>
	<artifactId>geocoder</artifactId>
	<version>2.99</version>
</dependency>
<dependency>
	<groupId>com.googlecode.libphonenumber</groupId>
	<artifactId>libphonenumber</artifactId>
	<version>8.9.14</version>
</dependency>

注:使用jar包获取需按需更新jar包版本
api形式参考了:https://blog.youkuaiyun.com/ljz2009y/article/details/39337273
但是原创的正则表达式匹配有问题;这里用简单形式重新完成

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值