Package com.ust.map;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.net.URL;
Import java.net.URLConnection;
Import Net.sf.json.JSONObject;
public class getaddressbyip{
Public String Getaddressbyip () {
Try
{
String StrIP = "60.15.28.2";
URL url = new URL ("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=" + 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){
SYSTEM.OUT.PRINTLN (result);
Jsonobject json = Jsonobject.fromobject (result.tostring ());
SYSTEM.OUT.PRINTLN (JSON);
String country= (String) json.get ("Country");
String province= (String) json.get ("province");
String city= (String) json.get ("City");
return country+province+city;
}
catch (IOException E)
{
Return "read failed";
}
}
public static void Main (string[] args) {
Getaddressbyip getaddressbyip=new Getaddressbyip ();
System.out.println (Getaddressbyip.getaddressbyip ());
}
}
Java access to national provincial city information according to IP