根据IP定位获取城市代码
2015-01-31 16:19
435 查看
public String getCityID() throws IOException{ URL url = new URL("http://61.4.185.48:81/g/"); HttpURLConnection urlcon = (HttpURLConnection) url.openConnection(); // 获取连接 InputStream is = urlcon.getInputStream(); BufferedReader buffer = new BufferedReader(new InputStreamReader(is)); StringBuffer bs = new StringBuffer(); String l = null; while ((l = buffer.readLine()) != null) { bs.append(l); } String getStr = bs.toString(); System.out.println(getStr); // var ip="121.33.190.178";var id=101280101;if(typeof(id_callback)!="undefined"){id_callback();} // 获取var id=后面的城市ID String cityID= getStr.substring(getStr.indexOf("id=") + 3, getStr.indexOf(";if")); return cityID; }
我们在浏览器输入
http://61.4.185.48:81/g/可以得到以下信息:
var ip="你当前外网IP";var id=101280101;if(typeof(id_callback)!="undefined"){id_callback();}
上述程序就是将“101280101”这个城市代码解析出来。
附:
中国天气网API:http://www.weather.com.cn/data/sk/101110101.html(其中101110101是城市代码)----->可行
http://www.weather.com.cn/data/cityinfo/101010100.html---------->可行 http://m.weather.com.cn/data/101110101.html(网页上显示的是JSON数据,但控制台打印输出不是JSON数据)
try{ URL url = new URL("http://www.weather.com.cn/data/sk/101110101.html"); URLConnection conn = url.openConnection(); BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));//转码。 String line = null; while ((line = reader.readLine()) != null) strBuf.append(line + " "); reader.close(); }catch(MalformedURLException e) { e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } String strJson= strBuf.toString();
相关文章推荐
- 根据手机ip地址获取城市,避免申请地图定位, String .split("\\s+")
- 根据IP获取城市代码
- 根据IP定位用户所在城市信息
- 根据IP定位用户所在城市信息
- 通过获取用户ip地址定位到用户的城市并显示
- 根据ip获取城市
- php根据ip获取地区的程序代码
- 根据ip获取城市名
- javaScript根据IP来获取城市等相关信息
- 根据IP定位用户所在城市信息
- php 根据ip获取城市以及网络运营商名称(利用qqwry.dat)
- PHP获取IP地址及根据IP判断城市实…
- 根据IP定位用户所在城市信息
- Winform-根据当前IP获取所在城市名称
- 【强大@推荐学习】PHP根据IP跳转不同城市代码
- PHP代码根据IP判断城市并跳转
- 根据IP定位用户所在城市信息
- 根据IP定位用户所在城市信息
- IOS-根据ip获取当前城市的编号
- 根据ip调用新浪api获取城市名并转成拼音