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. C#_数据库连接串的配置

    数据库的连接串 数据库连接字符串的拼写规则的决定条件: 连接的数据库的类型:SQL Server,Oracle,MySQL,Acess,MogoDB,Visual FoxPro(dBASE),Exce ...

  2. 常用的 文件 MIME类型

    估计很多朋友对不同后缀的文件对应的MIME类型不熟悉(实际上这么多我也记不住), 所以将平常常见的一些文件后缀对应的MIME类型写了一个对照表,现在奉献给大家: .asx,video/x-ms-asf ...

  3. Druid 数据库用户密码加密 代码实现

    druid-1.0.16.jar 阿里巴巴的开源数据连接池 jar包 明文密码+私钥(privateKey)加密=加密密码 加密密码+公钥(publicKey)解密=明文密码 程序代码如下: pack ...

  4. LinuxShell脚本攻略--第八章 当个好管家

    监视磁盘的使用情况 $ du file1.txt file2.txt $ du -a file_or_dir #-a递归输出指定目录的所有文件统计 $ du file_or_dir #这只是显示子目录 ...

  5. phalcon:数据库分库,读写分离,负载均衡 系统方法执行顺序

    phalcon:数据库分库,读写分离,负载均衡 系统方法执行顺序 用命名空间区分不同的数据库实例,对应代码结构上是不同的目录区分,在同一目录下基类负责初始化连接.连接来自初始化时注入的多个db服务 隐 ...

  6. 最牛B的编码套路(转)

    转自:http://blog.csdn.net/happydeer/article/details/17023229 最近,我大量阅读了Steve Yegge的文章.其中有一篇叫"Pract ...

  7. OneProxy使用手册--致力于打造透明的数据层

    介绍      平民软件官网上线(http://www.onexsoft.com) OneProxy是由原支付宝首席架构师楼方鑫开发,目前由楼方鑫创立的杭州平民软件公司(@平民架构)提供技术支持.目前 ...

  8. 从别人那淘的知识 深入剖析Java中的装箱和拆箱

    (转载的海子的博文   海子:http://www.cnblogs.com/dolphin0520/) 深入剖析Java中的装箱和拆箱 自动装箱和拆箱问题是Java中一个老生常谈的问题了,今天我们就来 ...

  9. HTML5自学笔记[ 12 ]canvas绘图小示例之鼠标画线

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  10. (15)odoo配置文件详解

    openerp-server.conf ---------------- [options]; addons模块的查找路径addons_path = E:\GreenOdoo8.0\source\op ...