首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Java 呼叫淘寶 API 查詢手機歸屬地並解析 JSON 回應

     編輯:Java 相關知識

整合淘寶手機號段 API 實作完整流程

淘寶提供公開的手機號段查詢介面(https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=),回應為 JavaScript 變數賦值格式,需經處理轉為標準 JSON 後解析。

核心實作類別

package com.think.java;

import java.io.*;
import java.net.*;
import java.util.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class TestMobileCity {

    /** 查詢單一手機號碼歸屬省份 */
    public static String calcMobileCity(String mobileNumber) throws MalformedURLException {
        String jsonString = null;
        JSONArray array = null;
        JSONObject jsonObject = null;
        
        String urlString = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=" + mobileNumber;
        StringBuffer sb = new StringBuffer();
        BufferedReader buffer = null;
        URL url = new URL(urlString);
        
        try {
            InputStream in = url.openStream();
            // 淘寶 API 回應編碼為 GB2312
            buffer = new BufferedReader(new InputStreamReader(in, "gb2312"));
            String line;
            while ((line = buffer.readLine()) != null) {
                sb.append(line);
            }
            in.close();
            buffer.close();
            
            jsonString = sb.toString();
            // 原始回應格式:__GetZoneResult_ = { ... } 
            // 移除前綴並包裝為 JSON 陣列
            jsonString = jsonString.replaceAll("^[__]\\w{14}+[_ = ]+", "[");
            String jsonString2 = jsonString + "]";
            
            array = JSONArray.fromObject(jsonString2);
            jsonObject = array.getJSONObject(0);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject.getString("province");
    }

    /** 批次查詢多個號碼 */
    public static JSONObject calcMobilesCities(List<String> mobileNumbers) throws MalformedURLException {
        JSONObject jsonNumberCity = new JSONObject();
        for (String mobileNumber : mobileNumbers) {
            jsonNumberCity.put(mobileNumber, calcMobileCity(mobileNumber));
        }
        return jsonNumberCity;
    }

    public static void main(String[] args) throws Exception {
        // 單一查詢測試
        String testMobile = "1881758452";
        System.out.println("歸屬地:" + calcMobileCity(testMobile));
        
        // 批次測試
        List<String> mobileList = new ArrayList<>();
        for (int i = 1350345; i < 1350388; i++) {
            mobileList.add(String.valueOf(i));
        }
        System.out.println("批次結果:" + calcMobilesCities(mobileList).toString());
    }
}

關鍵技術點解析

  1. 編碼處理:淘寶 API 回應為 gb2312,必須指定 InputStreamReader(in, "gb2312") 避免中文亂碼。
  2. JSON 格式修正:原始回應為 JS 代碼 __GetZoneResult_ = {province:'北京', ...},使用正則移除前綴並包裝成 [{...}] 供 JSONArray.fromObject() 解析。
  3. 依賴庫:使用 net.sf.json-lib (json-lib) 處理 JSON,Maven 依賴:
    <dependency>
        <groupId>net.sf.json-lib</groupId>
        <artifactId>json-lib</artifactId>
        <version>2.4</version>
        <classifier>jdk15</classifier>
    </dependency>
  4. 例外處理:網路請求需捕獲 MalformedURLException、IOException,建議加入超時設定與重試機制。

回應欄位說明

欄位說明範例
province省份北京
city城市北京
catName運營商中國移動
telString查詢號碼1881758452

生產環境建議

  • 快取查詢結果(如 Redis),減少外部 API 呼叫頻率
  • 使用連線池 HttpClient 替代 URLConnection 提升併發效能
  • 加入熔斷器(Hystrix/Resilience4j)防止下游失效拖垮服務
  • 注意淘寶 API 非官方承諾 SLA,關鍵業務建議採購專業號碼歸屬地資料庫
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved