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]