亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java實現(xiàn)根據(jù)ip地址獲取地理位置的代碼分享

 更新時間:2014年08月25日 10:15:44   投稿:junjie  
這篇文章主要介紹了java實現(xiàn)根據(jù)ip地址獲取地理位置的代碼分享,本文中使用的是QQ在線接口,也可以使用新浪、淘寶等提供的在線接口,需要的朋友可以參考下

前幾天想給網(wǎng)站后臺加個解析ip所在地理位置的功能,在網(wǎng)上看了一些博客,找了幾段程序,但總覺得寫的不夠簡潔,感覺很啰嗦。下面這個程序,感覺還算簡潔,于是整理調(diào)試了一下,可以用,程序調(diào)用了“騰訊ip分享計劃”提供的接口,當然也可以改成ip138提供的接口,不過這兩個網(wǎng)站返回的字符串格式有些不同,要分別做解析。

public String getAddressByIP()
{ 
  try
  {
    String strIP = "0.0.0.0";
    URL url = new URL( "http://ip.qq.com/cgi-bin/searchip?searchip1=" + strIP); 
    URLConnection conn = url.openConnection(); 
    BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "GBK")); 
    String line = null; 
    StringBuffer result = new StringBuffer(); 
    while((line = reader.readLine()) != null)
    { 
      result.append(line); 
    } 
    reader.close(); 
    strIP = result.substring(result.indexOf( "該IP所在地為:" ));
    strIP = strIP.substring(strIP.indexOf( ":") + 1);
    String province = strIP.substring(6, strIP.indexOf("省"));
    String city = strIP.substring(strIP.indexOf("省") + 1, strIP.indexOf("市"));
    ... ...
    ... ...
  }
  catch( IOException e)
  { 
    return "讀取失敗"; 
  }
}

附:

新浪接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=

淘寶接口:http://ip.taobao.com/service/getIpInfo.php?ip=[ip地址字串]

相關(guān)文章

最新評論