JAva携帯番号帰属地取得

24861 ワード

JAva携帯番号帰属地取得
ここではapiリクエストに基づいてjarパケットを利用して取得し、必要に応じていずれかを選択したり、両方を組み合わせて使用したりする2つの方法があります.
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("
"
); 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(" ",""); } 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.csdn.net/ljz2009y/article/details/39337273 しかし、オリジナルの正規表現のマッチングに問題があります.ここは簡単な形で作り直す