根据ip查询省市(来源淘宝提供的接口)

本文是根据淘宝提供的接口查询对应ip的所在省市等相关信息

代码如下:

 简单说明下,如果循环调用此方法查询,请每10000次,Thread.sleep(500),虽说接口不要钱,但也不能死心眼,把人家接口往死的调。(当然淘宝又不傻,如果你不休息,在一万多次的时候,接口会自己断开)

/**
	 * 查询一个ip所在的省市县.如果循环调用此方法,查询多个ip请每10000次Thread.sleep(500);(测试数据50万)
	 * @param strip
	 * @return 输出结果,{地区,省,市,县,运营商} 如果0表示没有
	 */
	public static String[] lookupIP(String strip) {
		String[] result = new String[5];
		if (strip == null || "".equals(strip.trim())) {
			return null;
		}
		BufferedReader reader = null;
		String line = null;
		try {
			URL url = new URL("http://ip.taobao.com/service/getIpInfo.php?ip=" + strip); // 访问php查询地址
			// InputStreamReader sr = new InputStreamReader(new FileInputStream(filepath), "utf-8");
			reader = new BufferedReader(new InputStreamReader(url.openStream(), "GBk"));
//			reader = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));
//			 返回的字符串,其中code的值的含义为,0:成功,1:失败
//			{"code":0,"data":{"ip":"210.75.225.254","country":"\u4e2d\u56fd","area":"\u534e\u5317",
//			"region":"\u5317\u4eac\u5e02","city":"\u5317\u4eac\u5e02","county":"","isp":"\u7535\u4fe1",
//			"country_id":"86","area_id":"100000","region_id":"110000","city_id":"110000",
//			"county_id":"-1","isp_id":"100017"}}
			while ((line = reader.readLine()) != null && !"".equals(line)) {
				if (line.startsWith("{\"code\":0")) {
					int beginIndex = line.indexOf("data\":{") + 7;
					line = line.substring(beginIndex);
					line.substring(0, line.length() - 3);
					line = line.replaceAll("}", "");
					line = line.replaceAll("\"", "");
					String[] data = line.split(",");
					Map<String,String> map = new HashMap<String,String>();
					for (int i = 0; i < data.length - 1; i++) {
						if (data[i].split(":").length > 1) {
							String name = data[i].split(":")[1];
							if(name.contains("\\u")){
								map.put(data[i].split(":")[0],vonvert(name.trim()));
							}else {
								map.put(data[i].split(":")[0],name.trim());
//								System.out.println(data[i].split(":")[0] + "\t"+ name.trim());
							}
						} else {
							map.put(data[i].split(":")[0],"0");
						}
					}
					for(int i=0; i<title.length; i++){
						result[i] = map.get(title[i]);
//						System.out.print(result[i]+"\t");
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (reader != null)
					reader.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}

上面代码中有个vonvert函数,调用返回的是十六进制的数据,需要转化

/**
	 * 把unicode转化为对应的汉字
	 * @param str
	 * @return
	 */
	private static String vonvert(String str){
		StringBuilder sb = new StringBuilder();
		int i= -1, pos = 0;
		while((i = str.indexOf("\\u",pos)) != -1){
			sb.append(str.subSequence(pos, i));
			if(i+5<str.length()){
				pos = i+6;
				sb.append((char)Integer.parseInt(str.substring(i+2,i+6),16));
			}
		}
		return sb.toString();
	}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值