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


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

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

    核心 Controller

    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 =
                    "13312341234," +
                            "15566667788," +
                            "19988776655," +
                            "18833445566," +
                            "18877776666"; // 你的电话号码列表
            return phoneNumberStr.split(",");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90

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

    结果截图:
    在这里插入图片描述

    完整源码可下载

  • 相关阅读:
    《流畅的python》— 列表推导与生成器表达式
    代码随想录训练营
    别乱用 FULL_CASE 和 PARALLEL_CASE
    聊聊并发编程——线程池
    实时通信:WebSocket
    【从零开始的刷题之旅】二叉搜索树(01):增删改查
    【ELK 使用指南 3】Zookeeper、Kafka集群与Filebeat+Kafka+ELK架构(附部署实例)
    对系统的 Go 版本进行升级
    无法解析 Failure to transfer *** from *** was cached in the local repository
    【C++和数据结构】位图和布隆过滤器
  • 原文地址:https://blog.csdn.net/qq12547345/article/details/133900726