根据IP定位获取城市代码
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定位获取城市代码的更多相关文章
- java解析XML获取城市代码
运行前先导入dom4j架包,由于我们公司用的代理服务器所以下面我设置了代理ip,不需要的可直接忽略 package com.chengshidaima.tools; import java.io.Bu ...
- C# 根据IP地址获取城市
using System; using System.IO; using System.Net; using System.Text; using System.Web.Script.Serializ ...
- 根据ip地址获取城市
var ip=context.Request.UserHostAddress; string url = "http://int.dpool.sina.com.cn/iplookup/ipl ...
- js根据ip地址获取城市地理位置
一.使用js根据ip获取地址位置 <script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>& ...
- java调用高德地图api实现通过ip定位访问者的城市
所需东西:高德地图的key 注意:这个key是 web服务的key 和js的key不是一个key(若没有则自行创建,创建教程在文末) 高德地图的api文档:https://lbs.amap.com/ ...
- 根据IP定位用户所在城市信息
http://www.9958.pw/post/city_ip 1.调用新浪IP地址库 新浪提供了开放的IP地址库数据供开发者调用,调用地址: http://int.dpool.sina.com.cn ...
- 根据用户IP获得所在城市
运行以下代码即可<html> <head> <meta http-equiv="Content-Type" content="text/ht ...
- 百度地图IP定位,点击地图添加marker
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- IP定位,天气接口
首先获取IP ////获得本地真实IP function get_onlineip() { $ip_json = @file_get_contents("http://ip.taobao.c ...
随机推荐
- golang执行linux命令
golang exec 执行系统命令 golang 2014-09-25 13:17:44 2779 0 0 exec.Command() 最简单的方法: cmd := exe ...
- 转:c++类实例在内存中的分配
转自:http://blog.csdn.net/alexwei2009/article/details/6157926 c++是一种面向对象的编程语言,它向下保持了对c的兼容,同时也允许程序员能够自由 ...
- aspx后缀映射成html
1.网站的配置文件添加如下代码: <configuration> <configSections> <section name="RewriterConfig& ...
- 通过NORFLASH中的uboot烧写uboot到nandFlash
在mini2440的教程中,在构建nandflash系统的时候是首先通过supervivi借助dnw烧写uboot.bin到nand flash 第零块, 由于我使用的是64位操作系统,usb驱动没安 ...
- eclipse js卡顿
http://blog.csdn.net/zhangzikui/article/details/24805935
- Now直播应用的后台服务器性能测试实践
版权声明:本文由Oliver原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/208 来源:腾云阁 https://www.q ...
- android动画小析
这里所讲的动画,是android framework提供的动画框架里面的动画. 是view层级的动画.不涉及到底层opengl es相关的动画实现. 动画: 主要包括 Interpolation du ...
- linux服务器挂载第二块磁盘图文解说
文章来源:http://www.cndns.com/help/help_con.aspx?hid=394 Linux磁盘挂载是比较常见的管理操作之一.我司橙云预装的linux系统有2块盘,一块为系统盘 ...
- HDU----(2157)How many ways??(快速矩阵幂)
How many ways?? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- 在汇编代码中调用C函数
对于ARM体系来说,不同语言撰写的函数之间相互调用(mix calls)遵循的是 ATPCS(ARM-Thumb Procedure Call Standard),ATPCS主要是定义了函数呼叫时参数 ...