国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java使用淘寶API讀寫json實現(xiàn)手機歸屬地查詢功能代碼

2019-11-26 15:53:06
字體:
供稿:網(wǎng)友

一般查詢手機歸屬地內(nèi)容應(yīng)該很好用json格式保存,在網(wǎng)上找到了淘寶的歸屬地API,并下了處理json相關(guān)的jar包,做了這個手機歸屬地查詢功能

復(fù)制代碼 代碼如下:

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_ = ”,讓它能轉(zhuǎn)換為JSONArray對象
            jsonString = jsonString.replaceAll("^[__]//w{14}+[_ = ]+", "[");
            // System.out.println(jsonString+"]");
            String jsonString2 = jsonString + "]";
            // 把STRING轉(zhuǎn)化為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());
    }
}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 沂水县| 克东县| 鄂伦春自治旗| 灵石县| 尉犁县| 铜川市| 米泉市| 东乌珠穆沁旗| 奉贤区| 遵义市| 玉树县| 逊克县| 宿迁市| 日照市| 汉阴县| 天祝| 宜良县| 锦州市| 泰顺县| 新平| 六枝特区| 洞头县| 炉霍县| 鹰潭市| 龙陵县| 澎湖县| 仁怀市| 出国| 太湖县| 团风县| 鄢陵县| 湄潭县| 临朐县| 江山市| 民乐县| 谷城县| 文水县| 慈溪市| 平乡县| 潮安县| 盐津县|