java 调用 360 接口实现批量查询手机号码归属地

网上的手机号码归属地查询,要么限制查询条数,要么收费,于是找到一个 360 提供的查询 api

使用多线程异步查询,Future 确保查询结果顺序与输入顺序一致

核心 Controller

java 复制代码
package com.example.phonenumber.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

@RestController
@RequestMapping("/api")
public class PhoneController {

    @Autowired
    private RestTemplate restTemplate;

    private static final int THREAD_POOL_SIZE = 8;

    @GetMapping("/search")
    public String search() {

        String url = "http://cx.shouji.360.cn/phonearea.php?number=";

        String[] phoneNumberList = getPhoneNumberList();

        ExecutorService executorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
        List<Future<Map<String, String>>> futures = new ArrayList<>();

        for (String number : phoneNumberList) {
            Future<Map<String, String>> future = executorService.submit(() -> processPhoneNumber(url, number));
            futures.add(future);
        }

        // 等待所有任务完成
        for (Future<Map<String, String>> future : futures) {
            try {
                Map<String, String> result = future.get();
                // 在这里,你可以将结果保存起来,比如放到一个 List 中
                System.out.println(result.get("number") + "\t" + result.get("province") + "\t" + result.get("city") + "\t" + result.get("sp"));
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace(); // 在实际应用中需要适当处理异常
            }
        }
        executorService.shutdown();
        return "success";
    }
    
    // 调用接口查询
    private Map<String, String> processPhoneNumber(String url, String number) {
        String response = restTemplate.getForObject(url + number, String.class);
        JSONObject responseJson = JSONUtil.parseObj(response);

        Map<String, String> result = new HashMap<>();
        result.put("number", number);
        // 解析响应数据
        if (responseJson.getInt("code") == 0) {
            JSONObject dataJson = responseJson.getJSONObject("data");
            // 省份
            result.put("province", dataJson.getStr("province"));
            // 城市
            result.put("city", dataJson.getStr("city"));
            // 运营商
            result.put("sp", dataJson.getStr("sp"));
        } else {
            result.put("error", "电话号码有误");
        }
        return result;
    }

    // 电话号码列表,以逗号分隔
    private String[] getPhoneNumberList() {
        String phoneNumberStr =
                "15649408889," +
                        "13333857377," +
                        "18898135919," +
                        "15037601625," +
                        "15890350177," +
                        "15890350177," +
                        "13673379352," +
                        "13673379352," +
                        "13849159230," +
                        "18937807559," +
                        "15649408889," +
                        "13333857377"; // 你的电话号码列表
        return phoneNumberStr.split(",");
    }
}

浏览器访问 http://localhost:8080/api/search

结果截图:

完整源码可下载

相关推荐
数智工坊6 小时前
【数据结构-栈】3.1栈的顺序存储-链式存储
java·开发语言·数据结构
短剑重铸之日6 小时前
《设计模式》第七篇:适配器模式
java·后端·设计模式·适配器模式
R-G-B6 小时前
python 验证每次操作图片处理的顺序是否一致,按序号打上标签,图片重命名
开发语言·python·图片重命名·按序号打上标签·验证图片处理的顺序
小二·6 小时前
Go 语言系统编程与云原生开发实战(第10篇)性能调优实战:Profiling × 内存优化 × 高并发压测(万级 QPS 实录)
开发语言·云原生·golang
DFT计算杂谈6 小时前
VASP+Wannier90 计算位移电流和二次谐波SHG
java·服务器·前端·python·算法
多多*6 小时前
2月3日面试题整理 字节跳动后端开发相关
android·java·开发语言·网络·jvm·adb·c#
无名的小白6 小时前
openclaw使用nginx反代部署过程 与disconnected (1008): pairing required解决
java·前端·nginx
xyq20246 小时前
jEasyUI 自定义分页
开发语言
.ZGR.6 小时前
认识数据结构:图——无人机防空平台的“衍生品”
java·开发语言·数据结构
huidu016 小时前
基于AQS实现的ReentrantLock
java