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. J2EE 第二阶段项目之编写代码(六)

    三张表的增 修改 查看.明天可以完成. 周末继续统计.

  2. 转!!!Mybatis实现数据的增删改查(CRUD)

    什么是 MyBatis? MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架. MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及对结果集的检索.MyBat ...

  3. 【c++】读写txt

    #include<iostrea> #include<fstream> int main() { ofstream fout;// 创建一个ofstream对象 fout.op ...

  4. hdu 2570

    贪心的经典题型 该死的精度问题,WA了好几次,以后能用乘的绝不用除!! #include<iostream> #include<algorithm> #include<c ...

  5. tr设置背景图片

    tr是不能设置背景图片的....

  6. 小程序---根据数据库反向生成java文件

    工作中写entry太繁琐,写了一个小程序反向生成.从而大大减少了工作量 import java.io.File; import java.io.FileWriter; import java.io.I ...

  7. 你不知道的JavaScript--值得你挑战的JavaScript面试题(45题)

    1,以下表达式的运行结果是: ["1","2","3"].map(parseInt) A.["1","2&qu ...

  8. 一个.net mvc的例子

    控制器 ( Controller) Product 下面功能主要根据多条件搜索产品的列表的功能 public ActionResult ProductList(string cityID, strin ...

  9. 在jsp页面解析json的2种方法

    方法1: $(function() { $("#btn").click(function() { $.ajax({ url : "fastjson.do", s ...

  10. Oracle后台进程

    后台进程简介 启动例程时,Oracle不仅会分配SGA,还会启动后台进程:关闭例程时,Oracle不仅会释放SGA所占用的内存空间,而且还会释放后台进程所占用的Cpu和内存资源.Oracle提供了很多 ...