Java implementation to obtain geographic location based on IP address code sharing _java

Source: Internet
Author: User
Tags string format stringbuffer

A few days ago to the Web site to add a resolution IP location of the function, on the Internet to see some blog, find a few procedures, but always feel that writing is not concise enough to feel very long-winded. The following procedure, the feeling is also concise, so the collation debugging, you can use, the program calls the "Tencent IP Sharing Program" provided by the interface, of course, can also be changed to ip138 provided interface, but the two Web site returned string format is somewhat different, to be resolved separately.

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 ("The IP location is:"));
    StrIP = strip.substring (Strip.indexof (":") + 1);
    String province = strip.substring (6, Strip.indexof ("province"));
    String City = strip.substring (Strip.indexof ("province") + 1, strip.indexof ("City"));
    ... ...
    ... ...
  }
  catch (IOException e)
  {return 
    "read failed"; 
  }
}

Report:

Sina Interface: http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=

Taobao Interface: Http://ip.taobao.com/service/getipinfo.php?ip=[ip address string]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.