public static String getCityName(){
try {
String path="http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js";
URL url = new URL(path);
//get //post
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(15*1000);
InputStream inStream = conn.getInputStream();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len = inStream.read(buffer)) !=-1 ){
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
outStream.close();
inStream.close();
String s=new String(data, "gb2312");
if(!s.contains("{"))
return null;
JSONObject item =new JSONObject(s.substring(s.indexOf("{"),s.length()-1));
return item.getString("city");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
try {
String path="http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js";
URL url = new URL(path);
//get //post
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(15*1000);
InputStream inStream = conn.getInputStream();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len = inStream.read(buffer)) !=-1 ){
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
outStream.close();
inStream.close();
String s=new String(data, "gb2312");
if(!s.contains("{"))
return null;
JSONObject item =new JSONObject(s.substring(s.indexOf("{"),s.length()-1));
return item.getString("city");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}