這里使用的是淘寶的接口
public class AddressUtil{
/**
*
* @param content
* 請求的參數 格式為:name=xxx&pwd=xxx
* @param encodingString
* 服務器端請求編碼。如GBK,UTF-8等
* @return
* @throws UnsupportedEncodingException
*/
public static String getAddresses(String content, String encodingString){
//調用淘寶API
String urlStr = "http://ip.taobao.com/service/getIpInfo.php";
String returnStr = getResult(urlStr, content,encodingString);
if(returnStr != null){
System.out.println(returnStr);
return returnStr;
}
return null;
}
/**
* @param urlStr
* 請求的地址
* @param content
* 請求的參數 格式為:name=xxx&pwd=xxx
* @param encodingString
* 服務器端請求編碼。如GBK,UTF-8等
* @return
*/
private static String getResult(String urlStr, String content, String encodingString) {
URL url = null;
HttpURLConnection connection = null;
try {
url = new URL(urlStr);
// 新建連接實例
connection = (HttpURLConnection) url.openConnection();
// 設置連接超時時間,單位毫秒
//connection.setConnectTimeout(20000);
// 設置讀取數據超時時間,單位毫秒
//connection.setReadTimeout(20000);
//是否打開輸出流
connection.setDoOutput(true);
//是否打開輸入流
connection.setDoInput(true);
//提交方法 POST|GET
connection.setRequestMethod("POST");
//是否緩存
connection.setUseCaches(false);
//打開連接端口
connection.connect();
//打開輸出流往對端服務器寫數據
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
//寫數據,即提交表單 name=xxx&pwd=xxx
out.writeBytes(content);
//刷新
out.flush();
//關閉輸出流
out.close();
// 往對端寫完數據對端服務器返回數據 ,以BufferedReader流來讀取
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), encodingString));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null){
buffer.append(line);
}
reader.close();
return buffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(connection != null){
connection.disconnect();
}
}
return null;
}
public static Map<String, String> getAddressByIp(String ip){
// 參數ip
// String ip = "27.40.147.229";
// json_result用於接收返回的json數據
String json_result = null;
Map<String, String> map=new HashMap<String, String>();
try {
json_result =getAddresses("ip=" + ip, "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
JSONObject json = JSONObject.fromObject(json_result);
System.out.println("json數據: " + json);
String country = JSONObject.fromObject(json.get("data")).get("country").toString();
String region = JSONObject.fromObject(json.get("data")).get("region").toString();
String city = JSONObject.fromObject(json.get("data")).get("city").toString();
String county = JSONObject.fromObject(json.get("data")).get("county").toString();
String isp = JSONObject.fromObject(json.get("data")).get("isp").toString();
String area = JSONObject.fromObject(json.get("data")).get("area").toString();
System.out.println("國家: " + country);
System.out.println("地區: " + area);
System.out.println("省份: " + region);
System.out.println("城市: " + city);
System.out.println("區/縣: " + county);
System.out.println("互聯網服務提供商: " + isp);
map.put("country", country);//國家
map.put("area", area);//區域
map.put("region", region);//省
map.put("city", city);//市
map.put("county", county);//區
map.put("isp", isp);//互聯網服務提供商
return map;
}
}
注:以上代碼轉自https://www.cnblogs.com/xym4869/p/8995504.html(不是本人原創)