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

网友投稿 398 2022-06-06


想了解java使用淘宝API读写json实现手机归属地查询功能代码的相关内容吗,在本文为您仔细讲解java手机归属地查询功能的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:淘宝API,手机归属地查询,json,java,下面大家一起来学习吧。

一般查询手机归属地内容应该很好用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());
    }
}



版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:python翻译软件 python翻译软件实现代码(使用google api完成)
下一篇:c#打开我的电脑和获取电脑驱动器信息 c#不使用windows api函数打开我的电脑和获取电脑驱动器信息(cp是什么意思?)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~