栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

   
    public static JSonObject calcMobilesCities(List 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 mobileList = new ArrayList();
        for(int i = 1350345; i < 1350388; i++){
            mobileList.add(String.valueOf(i));
        }
        System.out.println(calcMobilesCities(mobileList).toString());
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/152829.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号