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(",");
    }
}

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

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

完整源码可下载

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
适配机型/应用平台 Java/S40 适用机型:诺基亚 7610S; 7510a; 7310c; 7070; 6103; 5000; 3120c; 2680s; 2600c; 1680c; 5220xm; 3600s; 7360; 7380; 6070; 8800 SE; 5300; 6088; 8800 Sirocco; 6086; 8600 Luna; 3500c; 7900; 6131i; 6263; 7500; 8800 Arte; 8600; 7390; 7373; 7370; 6555; 6288; 6280; 6275; 6267; 6165; 6151; 6136; 6133; 6131; 6126; 6125; 6111; 6085; 6080; 6060; 6021; 5070; 3500; 3110; 3109; 2760; 2630; 2626; 2610; 6301; 5610xm; 5310xm; 6108; 7260; 6270; 6030; 6020; 3220; 3120; 2865; 6300; 6234; 6233; 5200; 6500c; 6500s; 7600; 7210; 7200; 6800; 6650; 6610; 6230; 6220; 6100; 5100; 3300; 3200; 3100 摩托罗拉 E398; EM25; EM28; K3; KRZR; L6; L6g; L800t; Pebl U3; RAZR maxx V1100; ROKR W5; SLVR L6i; SLVR L7; SLVR L71; SLVR L72; V3; V300; V303; V500; V501; V600; VE75; W206; W230; W270; W360; W380; W388; W395; W396; W510; W6; ZN200; w231 索尼爱立信 C702; C902; G502; K660i; R306c; T280i; T303c; W350c; W760c; Z550c; Z558c; Z600; Z608; Z610i; Z780 三星 D528; D808; D828; D838; D848; D888; D908; D908i; D988; E218; E258; E428; E598; E848; E878; E898; E908; E958; F218; F258; F268; F278; F308; F488E; G608; G618; G800; G808E; J608; J618; J708; M318; M618; M628; Soulb; U308; U600; U608; U708E; U800; U808E; U900; U908E; W579; W629; X838 联想 E520; P705; P719; P790; P960; P990; S320; S530; S6; S600; S7; S70; S9; V80; i510; i758; i760; i817; i827; i880; i906; i909; i966; i968; p609; s90; s96 夏新 A510; A530; A616; A636; E3; E5; M515; M690 天语 A615; A902; A905; A906; A908; A909; A930; B922; C280; C800; D705; E60
好的,我理解您的问题,您的要求是通过程序自动批量查询手机号归属地,并将查询结果保存到文档中,同时不使用第三方接口。我有一个思路可以实现您的需求: 1. 从文档中读取手机号码数据。 2. 编写一个程序,通过手机号码中的前7位数字,匹配到对应的手机号段,然后根据手机号查询手机号归属地。 3. 将查询结果保存到文档中。 以下是一个简单的实现代码示例,仅供参考: ```python import xlrd import xlwt # 打开要读取的文档 workbook = xlrd.open_workbook('mobile_numbers.xls') # 打开要写入的文档 write_book = xlwt.Workbook() # 添加一个sheet页 sheet1 = write_book.add_sheet('mobile_numbers') # 读取第一个sheet页 sheet = workbook.sheet_by_index(0) # 逐行遍历手机号码数据 for i in range(sheet.nrows): # 获取手机号码 mobile_number = sheet.cell_value(i, 0) # 获取手机号码前7位数字 prefix = mobile_number[0:7] # 根据手机号查询归属地,这里可以自己实现查询逻辑 location = query_location_by_prefix(prefix) # 将查询结果保存到新文档中 sheet1.write(i, 0, mobile_number) sheet1.write(i, 1, location) # 保存新文档 write_book.save('mobile_numbers_location.xls') ``` 这段代码中,`query_location_by_prefix` 函数是一个示例函数,需要根据实际情况自行实现。这个函数的作用是根据手机号查询归属地,返回归属地信息。可以使用一些公开的手机号归属查询接口或者本地的手机号归属地数据库等方式实现

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值