根據手機號獲取手機號歸屬地


前言

我們在做開發的時候,可能會遇到根據手機號反推出歸屬地的問題,所以就找了寫資料,反推只能精確到省份。

    /**
     * 獲取URL返回的字符串
     *
     * @param callurl
     * @param charset
     * @return
     */
    private static String callUrlByGet(String callurl, String charset) {
        String result = "";
        try {
            URL url = new URL(callurl);
            URLConnection connection = url.openConnection();
            connection.connect();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    connection.getInputStream(), charset));
            String line;
            while ((line = reader.readLine()) != null) {
                result += line;
                result += "\n";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        return result;
    }

    /**
     * 手機號碼歸屬地
     *
     * @param tel 手機號碼
     * @return 135XXXXXXXX, 聯通/移動/電信,湖北武漢
     * @throws Exception
     * @author
     */
    public static String getMobileLocation(String tel) throws Exception {
        Pattern pattern = Pattern.compile("1\\d{10}");
        Matcher matcher = pattern.matcher(tel);
        if (matcher.matches()) {
            String url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=" + tel;
            String result = callUrlByGet(url, "GBK").replace("__GetZoneResult_ =", "");
            HashMap hashMap = JSON.parseObject(result, HashMap.class);
            String province = hashMap.get("province").toString();
            if ("".equals(province)){
                return "無此號記錄!";
            }else {
                return province;
            }
        }
        return "無此號記錄!";

    }

  用的時候直接調用即可。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM