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. 转:最小区间:k个有序的数组,找到最小区间使k个数组中每个数组至少有一个数在区间中

    转:http://www.itmian4.com/thread-6504-1-1.html 最小区间原题 k个有序的数组,找到最小的区间范围使得这k个数组中,每个数组至少有一个数字在这个区间范围内.比 ...

  2. scala调用java的方法,返回了一个对象链表List<Student>,在scala中遍历该链表获取指定Student的名字name

    假设Student类如下: class Student { private int no; private String name; public int getNo() { return no; } ...

  3. python网络编程socket之多进程

    #coding:utf-8 __author__ = 'similarface' import os,socket,threading,SocketServer SERVER_HOST='localh ...

  4. 解决Tomcat: Can't load IA 32-bit .dll on a AMD 64-bit platform问题

    http://201209235316.iteye.com/blog/2063853 http://www.cnblogs.com/liuyp-ken/p/5503822.html

  5. 清理PC垃圾

    @echo off pause echo 正在清除系统垃圾文件,请稍等...... del /f /s /q %systemdrive%\*.tmp del /f /s /q %systemdrive ...

  6. Rest接口中正则表达式

    匹配所有字符: @Path("/server/{urlParam: .*}")

  7. 5.4.2 使用配置启动firefox

    1.使用firefox的本地配置加载浏览器 使用本地配置加载浏览器,代码如下. 这样运行后,可以看到firebug等插件都已启动. 练习:假设做性能测试时,需要获取某个状况下的页面网络运行参数.要求完 ...

  8. 四位专家分享:18个网站SEO建议

    搜索引擎优化(简称SEO)对于互联网新创企业来说很重要.下面是四位相关专家给出的建议. 第一位专家是Autotrader公司的搜索市场经理Dewi Nawasari,她认为SEO就是优化网站,以吸引你 ...

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

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

  10. 关于64位WIN7下正确建立JAVA开发环境(转

    1.下载并安装JDK(地址:http://www.oracle.com/technetwor ... ownload-400750.html     先在“Accept License Agreeme ...