Flask接口签名sign原理与实例代码浅析
398
2022-06-06
一般查询手机归属地内容应该很好用json格式保存,在网上找到了淘宝的归属地API,并下了处理json相关的jar包,做了这个手机归属地查询功能
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小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~