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定位获取城市代码的更多相关文章

  1. java解析XML获取城市代码

    运行前先导入dom4j架包,由于我们公司用的代理服务器所以下面我设置了代理ip,不需要的可直接忽略 package com.chengshidaima.tools; import java.io.Bu ...

  2. C# 根据IP地址获取城市

    using System; using System.IO; using System.Net; using System.Text; using System.Web.Script.Serializ ...

  3. 根据ip地址获取城市

    var ip=context.Request.UserHostAddress; string url = "http://int.dpool.sina.com.cn/iplookup/ipl ...

  4. js根据ip地址获取城市地理位置

    一.使用js根据ip获取地址位置 <script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>& ...

  5. java调用高德地图api实现通过ip定位访问者的城市

    所需东西:高德地图的key 注意:这个key是 web服务的key  和js的key不是一个key(若没有则自行创建,创建教程在文末) 高德地图的api文档:https://lbs.amap.com/ ...

  6. 根据IP定位用户所在城市信息

    http://www.9958.pw/post/city_ip 1.调用新浪IP地址库 新浪提供了开放的IP地址库数据供开发者调用,调用地址: http://int.dpool.sina.com.cn ...

  7. 根据用户IP获得所在城市

    运行以下代码即可<html> <head> <meta http-equiv="Content-Type" content="text/ht ...

  8. 百度地图IP定位,点击地图添加marker

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  9. IP定位,天气接口

    首先获取IP ////获得本地真实IP function get_onlineip() { $ip_json = @file_get_contents("http://ip.taobao.c ...

随机推荐

  1. [CSS] vertical-align

    原文地址: http://www.zhangxinxu.com/wordpress/2010/05/%E6%88%91%E5%AF%B9css-vertical-align%E7%9A%84%E4%B ...

  2. linux登录mysql

    mysql  -u 用户名 -p密码 mysql -u root -psqj888

  3. xmpp4-总览

    1注意点 创建的聊天室本地缓存,和服务器存储有时不同步,似乎聊过天就更稳定一点.

  4. hiho_1290_demo_day

    题目大意 一个MxN的矩阵,矩阵中的有些方格中有障碍物,有些没有,有一个机器人从左上角出发,它只能有两种移动方式:一直向右移动,直到遇到障碍物:一直向下移动,直到遇到障碍物.     现在可以将矩阵中 ...

  5. 基于SpringBoot项目的https

    在spring中配置项目运行的端口很简单. 在application.properties中 server.port: 这样配置后,spring boot内嵌的tomcat服务器就是跑在8080端口启 ...

  6. php中的 == 和 ===

    == 是等值 1 和  ‘1’ 是相等的 === 要等值并且类型相等,比如 1 和  ‘1’ 是不相等的,只有 ‘1’ 和 ‘1’ 是相等的.哈哈哈. http://ihacklog.com/post ...

  7. Windows7睡眠后自动唤醒

    笔者的电脑(Windows7 64位旗舰版)睡眠后,隔段时间后会自动唤醒.经两项配置后,解决了该问题. 1 禁用唤醒定时器 控制面板里进入"电源选项""更改计划设置&qu ...

  8. css制作简单的导航栏

    //css代码 #menu{ height: 65px; width:100%; background-color: rgba(0, 0, 0, 0.5);}#menu ul{ list-style: ...

  9. [saiku] 将saiku自带的H2嵌入式数据库迁移到本地mysql数据库

    saiku数据库的表和用户默认创建是在启动项目的时候,通过初始化 saiku-beans.xml 中的 h2database 这个 bean 执行org.saiku.service.Database类 ...

  10. SyntaxError: Non-ASCII character '\xe7' in file解决方法

    SyntaxError: Non-ASCII character '\xe7' in file 出现这种错误的原因是程序中的编码出问题了,只要在程序的最前面加上 最前面的意思是在最前面,包括在注释的前 ...