java使用淘宝API读写json实现手机归属地查询功能代码

时间:2021-05-31 03:27:38 

一般查询手机归属地内容应该很好用json格式保存,在网上找到了淘宝的归属地API,并下了处理json相关的jar包,做了这个手机归属地查询功能


package com.think.java;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class TestMobileCity {   

    /**
     * 测试手机号码是来自哪个城市的,利用淘宝的API
     * @param mobileNumber 手机号码
     * @return
     * @throws MalformedURLException
     */
    public static String calcMobileCity(String mobileNumber) throws MalformedURLException{
        String jsonString = null;
        JSONArray array = null;
        JSONObject jsonObject = null;
        String urlString = "http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=" + mobileNumber;
        StringBuffer sb = new StringBuffer();
        BufferedReader buffer;
        URL url = new URL(urlString);
        try{
            InputStream in = url.openStream();

            // 解决乱码问题
            buffer = new BufferedReader(new InputStreamReader(in,"gb2312"));
            String line = null;
            while((line = buffer.readLine()) != null){
                sb.append(line);
            }
            in.close();
            buffer.close();
            // System.out.println(sb.toString());
            jsonString = sb.toString();
            // 替换掉“__GetZoneResult_ = ”,让它能转换为JSONArray对象
            jsonString = jsonString.replaceAll("^[__]\\w{14}+[_ = ]+", "[");
            // System.out.println(jsonString+"]");
            String jsonString2 = jsonString + "]";
            // 把STRING转化为json对象
            array = JSONArray.fromObject(jsonString2);

            // 获取JSONArray的JSONObject对象,便于读取array里的键值对
            jsonObject = array.getJSONObject(0);       

        }catch(Exception e){
            e.printStackTrace();
        }
        return jsonObject.getString("province");
    }

    /**
     * 计算多个号码的归属地
     * @param mobileNumbers 号码列表
     * @return
     * @throws MalformedURLException
     */
    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 testMobileNumber = "1881758452";
        System.out.println(calcMobileCity(testMobileNumber));
        List<String> mobileList = new ArrayList<String>();
        for(int i = 1350345; i < 1350388; i++){
            mobileList.add(String.valueOf(i));
        }
        System.out.println(calcMobilesCities(mobileList).toString());
    }
}

标签:淘宝API,手机归属地查询,json,java
0
投稿

猜你喜欢

  • IDEA设置背景为自定义照片的操作方法

    2022-12-28 09:13:08
  • JFreeChart插件实现的折线图效果实例

    2023-09-21 02:20:03
  • java创建线程的两种方法区别

    2023-11-11 09:17:52
  • Flutter 剪裁组件的使用

    2023-06-18 13:15:04
  • Java中ShardingSphere分库分表实战

    2023-11-24 09:20:37
  • Java实现word/pdf转html并在线预览

    2022-09-09 09:16:49
  • Mybatis核心组成部分之SQL映射文件揭秘详解

    2023-08-22 18:45:21
  • Java中StringUtils与CollectionUtils和ObjectUtil概念讲解

    2023-11-29 07:45:38
  • Spring Boot 详细分析Conditional自动化配置注解

    2021-11-25 21:56:14
  • 将JavaDoc注释生成API文档的操作

    2023-06-16 18:24:06
  • Java项目中获取路径的绝对路径问题和相对路径问题

    2023-07-09 13:25:01
  • Android 文件数据存储实例详解

    2023-07-28 17:08:03
  • Activiti如何启动流程并使流程前进

    2023-11-18 18:38:51
  • java源码解析之String类的compareTo(String otherString)方法

    2023-11-11 23:10:00
  • java设计模式--原型模式详解

    2023-11-25 05:08:24
  • 解决SpringBoot web项目启动后立即关闭的问题

    2023-07-26 02:33:37
  • springboot使用Logback把日志输出到控制台或输出到文件

    2022-05-30 17:34:30
  • Java使用Tess4J实现图像识别方式

    2022-10-07 19:24:11
  • SpringBoot打Jar包在命令行运行流程详解

    2023-11-24 16:53:59
  • Java多线程wait()和notify()方法详细图解

    2021-09-19 20:27:32
  • asp之家 软件编程 m.aspxhome.com