根据ip来获取当前城市地点(仅仅是百度的接口)

根据ip获取城市地点等网上有很多中方法,总结起来就两种,一是在线请求,二是离线本地集成,各有各的好,在这里记录一下第一种情况的其中一种(百度:http://apis.baidu.com/apistore/iplookupservice/iplookup);

apikey:127329af2783917a946abd0f8479f571(这样算不算重点符号,语文有可能是英语老师教的哈)

    /**
	 *  百度开放接口通过IP地址获取地理位置
	 * 
	 * @return 返回结果 String[]
	 */
	public static String getAddressByIp(String ip) {
		if (ip == null || ip.equals("")) {
			return null;
		}
		BufferedReader reader = null;
		String result = null;
		StringBuffer sbf = new StringBuffer();
		String thisUrl = httpUrl + "?ip=" + ip;
		try {
			URL url = new URL(thisUrl);
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			connection.setRequestMethod("GET");
			connection.setRequestProperty("apikey",
					"127329af2783917a946abd0f8479f571");
			connection.connect();
			InputStream is = connection.getInputStream();
			reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			String strRead = null;
			while ((strRead = reader.readLine()) != null) {
				sbf.append(strRead);
				sbf.append("\r\n");
			}
			reader.close();
			result = sbf.toString();
			return=result ;

		} catch (Exception e) {
			SystemOut.println("获取IP地址失败");
		}
		return null;
	}

***:开始了吗? wo:已经结束了、、、、、、、、此处省去n多种想法。

给个大神连接:https://my.oschina.net/wangmengjun/blog/815697

你可能感兴趣的:(前端,后台)