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());
}
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
IDEA设置背景为自定义照片的操作方法
![](https://img.aspxhome.com/file/2023/5/61645_0s.jpg)
JFreeChart插件实现的折线图效果实例
![](https://img.aspxhome.com/file/2023/0/60810_0s.jpg)
java创建线程的两种方法区别
Flutter 剪裁组件的使用
![](https://img.aspxhome.com/file/2023/3/96783_0s.png)
Java中ShardingSphere分库分表实战
![](https://img.aspxhome.com/file/2023/7/59767_0s.jpg)
Java实现word/pdf转html并在线预览
![](https://img.aspxhome.com/file/2023/5/65635_0s.png)
Mybatis核心组成部分之SQL映射文件揭秘详解
Java中StringUtils与CollectionUtils和ObjectUtil概念讲解
![](https://img.aspxhome.com/file/2023/4/59004_0s.png)
Spring Boot 详细分析Conditional自动化配置注解
![](https://img.aspxhome.com/file/2023/9/61799_0s.png)
将JavaDoc注释生成API文档的操作
![](https://img.aspxhome.com/file/2023/1/61151_0s.png)
Java项目中获取路径的绝对路径问题和相对路径问题
![](https://img.aspxhome.com/file/2023/2/59902_0s.jpg)
Android 文件数据存储实例详解
Activiti如何启动流程并使流程前进
![](https://img.aspxhome.com/file/2023/9/59489_0s.png)
java源码解析之String类的compareTo(String otherString)方法
java设计模式--原型模式详解
![](https://img.aspxhome.com/file/2023/0/60190_0s.png)
解决SpringBoot web项目启动后立即关闭的问题
![](https://img.aspxhome.com/file/2023/9/64909_0s.png)
springboot使用Logback把日志输出到控制台或输出到文件
![](https://img.aspxhome.com/file/2023/3/63763_0s.png)
Java使用Tess4J实现图像识别方式
![](https://img.aspxhome.com/file/2023/9/60659_0s.png)
SpringBoot打Jar包在命令行运行流程详解
![](https://img.aspxhome.com/file/2023/8/59548_0s.png)
Java多线程wait()和notify()方法详细图解
![](https://img.aspxhome.com/file/2023/1/66231_0s.png)