JAVA 调用淘宝IP地址库API

接口说明

调用方法:利用json 解析返回的结果

可能要导入的包有:ezmorph-1.0.6.jar   json-lib-2.2.3-jdk15.jar

 

 

	public static void main(String[] args)  {

		System.out.println(getIpArea("183.62.35.197"));

	}
	
	public static String getIpArea(String ip){
		
		String path = "http://ip.taobao.com/service/getIpInfo.php?ip="+ip;
		String inputline="";
		String info="";
		
		try {
			URL url = new URL(path);
			HttpURLConnection conn = (HttpURLConnection)url.openConnection();
			conn.setReadTimeout(10*1000);
			conn.setRequestMethod("GET");
			
			InputStreamReader  inStream = new InputStreamReader(conn.getInputStream(),"UTF-8");
			BufferedReader buffer=new BufferedReader(inStream);
			 
			while((inputline=buffer.readLine())!=null){
				info+=inputline;
			}
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		JSONObject jsonob = JSONObject.fromObject((JSONObject.fromObject(info).getString("data")));
	    String city = StringEscapeUtils.escapeSql(jsonob.getString("city"));
	    
	    return city;
	}
	


 

你可能感兴趣的:(JAVA)