URL url = new
URL("http://ip.taobao.com/service/getIpInfo.php?ip="+"27.223.68.94");
HttpURLConnection connection = (HttpURLConnection)
url.openConnection(); connection.setDoOutput(true);
connection.setDoInput(true); connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.connect();
JSONObject jsono = new JSONObject(new JSONTokener(
new InputStreamReader(connection.getInputStream())));
String city = "";
String rep = jsono.toString();
String[] s = rep.split(",");
for (int i = 0; i < s.length; i++) {
if (s[i].contains("市")) {
if (s[i].length() > 8) {
city = s[i].substring(8, s[i].length() - 2);
break;
} else {
city = "";
}
}
}
System.out.println(city);
转载于:https://my.oschina.net/angleshuai/blog/354876