lizongbo at 618119.com 工作,生活,Android,前端,Linode,Ubuntu,nginx,java,apache,tomcat,Resin,mina,Hessian,XMPP,RPC

2011年05月5日

从google的json接口获取地理位置信息的Java代码

Filed under: IP,Java,JavaScript — 标签:, , , , , — lizongbo @ 22:55

从google的json接口获取地理位置信息的Java代码

google 提供了公开的接口,通过,这个接口,根据gps或基站信息或wifi热点信息来获取当前位置的地理信息.
gps信息是经纬度,基站信息是基站的cellid等信息,wifi热点信息是wifi的mac地址是热点名字,信号强度等。
因此可以通过java程序获取相应信息,在LBS项目中可以用上:

api接口定义描述在:

http://code.google.com/p/gears/wiki/GeolocationAPI

Java代码如下:

[code]

package com.lizongbo.geo;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;

import com.lizongbo.org.json.JSONArray;
import com.lizongbo.org.json.JSONException;
import com.lizongbo.org.json.JSONObject;

/**
* 从google的json接口获取地理位置信息的代码
*
* @author lizongbo
*
*/
public class GeoTest {

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String url = “http://www.google.com/loc/json”;
JSONObject json = new JSONObject();
json.put(“version”, “1.1.0”);
json.put(“host”, “maps.618119.com”);
json.put(“home_mobile_country_code”, 460);// 国家代码
json.put(“home_mobile_network_code”, 0);// 移动运营商代码
json.put(“radio_type”, “gsm”);
json.put(“carrier”, “lizongbo”);
json.put(“request_address”, true);
json.put(“address_language”, “zh_CN”);
JSONArray jsoncells = new JSONArray();
json.put(“cell_towers”, jsoncells);
JSONArray jsonwifis = new JSONArray();
json.put(“wifi_towers”, jsonwifis);
JSONObject jsoncell = new JSONObject();
jsoncell.put(“mobile_country_code”, 460);// 国家代码,mcc
jsoncell.put(“mobile_network_code”, 0);// 移动运营商代码,mnc
jsoncell.put(“location_area_code”, 9364);// 位置区域代码,lac
jsoncell.put(“cell_id”, “3851”);// 移动基站id
// jsoncell.put(“age”, 0);
// jsoncell.put(“signal_strength”, -70);
// jsoncell.put(“timing_advance”, 7777);
jsoncells.put(jsoncell);
JSONObject jsonwifi = new JSONObject();
// jsonwifi.put(“mac_address”, “00-11-22-33-44-55”);
// jsonwifi.put(“signal_strength”, 8);
// jsonwifi.put(“age”, 0);
// jsonwifis.put(jsonwifi);
// jsonwifi = new JSONObject();
jsonwifi.put(“mac_address”, “00-55-44-33-22-11”);//
jsonwifi.put(“ssid”, “TPLINK_618119”);// 无线路由器的名字
jsonwifi.put(“signal_strength”, 8);// 信号强度
jsonwifi.put(“age”, 0);
// jsonwifis.put(jsonwifi);

System.out.println(json.toString());
System.out.println(downloadUrlbyPOST(url, json.toString(), null,
“UTF-8”));

}

public static String downloadUrlbyPOST(String urlStr, String query,
String referer, String encoding) throws Exception {
String line = “”;
StringBuilder sb = new StringBuilder();
HttpURLConnection httpConn = null;
try {
URL url = new URL(urlStr);
System.out.println(urlStr + “?” + query);
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
“proxy.lizongbo.com”, 8080));
proxy = Proxy.NO_PROXY;
httpConn = (HttpURLConnection) url.openConnection(proxy);
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
httpConn.setRequestMethod(“POST”);
if (referer != null) {
httpConn.setRequestProperty(“Referer”, referer);
}
httpConn.setConnectTimeout(5000);
// httpConn.getOutputStream().write(
// java.net.URLEncoder.encode(query, “UTF-8”).getBytes());
httpConn.getOutputStream().write(query.getBytes());
httpConn.getOutputStream().flush();
httpConn.getOutputStream().close();

BufferedReader in = null;
if (httpConn.getResponseCode() != 200) {
System.err.println(“error:” + httpConn.getResponseMessage());
in = new BufferedReader(new InputStreamReader(httpConn
.getErrorStream(), “UTF-8”));
} else {
in = new BufferedReader(new InputStreamReader(httpConn
.getInputStream(), “UTF-8”));
}
while ((line = in.readLine()) != null) {
sb.append(line).append(‘\n’);
}
// 关闭连接
httpConn.disconnect();
return sb.toString();
} catch (Exception e) {
// 关闭连接
httpConn.disconnect();
System.out.println(e.getMessage());
throw e;
}
}
}

[/code]
运行结果如下:
{“address_language”:”zh_CN”,”wifi_towers”:[],”host”:”maps.618119.com”,”radio_type”:”gsm”,”home_mobile_country_code”:9364,”carrier”:”lizongbo”,”home_mobile_network_code”:460,”cell_towers”:[{“mobile_network_code”:0,”cell_id”:”3851″,”mobile_country_code”:460,”location_area_code”:9364}],”request_address”:true,”version”:”1.1.0″}
http://www.google.com/loc/json?{“address_language”:”zh_CN”,”wifi_towers”:[],”host”:”maps.618119.com”,”radio_type”:”gsm”,”home_mobile_country_code”:9364,”carrier”:”lizongbo”,”home_mobile_network_code”:460,”cell_towers”:[{“mobile_network_code”:0,”cell_id”:”3851″,”mobile_country_code”:460,”location_area_code”:9364}],”request_address”:true,”version”:”1.1.0″}
{“location”:{“latitude”:22.522185,”longitude”:113.925618,”address”:{“country”:”中国”,”country_code”:”CN”,”region”:”广东省”,”city”:”深圳市”,”street”:”滨海大道辅路”},”accuracy”:620.0},”access_token”:”2:618119:lizongbo”}

试了一下把无线路由器的mac地址发到 https://www.google.com/loc/json ,google果然返回很准的定位看来google是把我用手机测试定位的时候就把数据采集分析好了,确实强大。

2010年12月23日

java.net.URL设置代理服务器和自定义Host头绕过DNS解析

Filed under: DNS,IP — 标签:, , , , , , , — lizongbo @ 00:34

在實際工作中,由于办公网络的限制,连接外网访问时需要指定代理,

而访问某些虚拟主机域名的服务器时,并不存在对应的dns解析记录,因此需要手工配置hosts文件来进行映射,非常麻烦。

于是根据java.net.URL的api整理了设置代理服务器和自定义Host头绕过DNS解析的方法:

代码如下:

[code]

public static void main(String[] args) throws IOException {
/**
* 下面这种方法是全局方式的设置 ,不建议使用。 System.setProperty(“proxySet”, “true”);
* System.setProperty(“proxyHost”, “proxy.lizongbo.com”);
* System.setProperty(“proxyPort”, “8080”);
*/
String urlStr = “http://about.me/lizongbo”;
urlStr = “http://about.me/lizongbo”;
HttpURLConnection httpConn = null;
StringBuilder sb = new StringBuilder();
URL url = new URL(urlStr);
String hostName = url.getHost();
// 根据配置自定义url里的ip地址
if (hostName.toLowerCase().endsWith(“.mqq.im”)) {
url = new URL(urlStr.replace(hostName, “127.0.0.1”));
}
System.out.println(url);
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
“proxy.lizongbo.com”, 8080));
// 只针对当前这个连接使用http代理
httpConn = (HttpURLConnection) url.openConnection(proxy);
httpConn.setRequestMethod(“GET”);
// 手工设置Host头信息以支持虚拟主机,通过这样的方式,可以避免去手工配置/etc/hosts来绕过dns解析的麻烦,尤其是程序经常在不同服务器上运行的时候,非常实用
httpConn.setRequestProperty(“Host”, hostName);
httpConn.setConnectTimeout(5000);
httpConn.connect();
InputStream is = httpConn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is,
“UTF-8”));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line).append(‘\n’);
}
// 关闭连接
httpConn.disconnect();
System.out.println(sb);
}

[/code]

2007年12月15日

深圳电信的http://219.133.33.37/update/step1.aspx?p

Filed under: DNS,IP — 标签:, , , — lizongbo @ 23:41

深圳电信对 ADSL上网的 DNS 劫持很过分,用firefox打开网页,每天晚上上一会儿网,就几乎几十次的出现这玩意,

总要去打开 219.133.33.37 的东西,无语!!!

类似情况可参考:

http://www.williamlong.info/archives/1116.html

[code]

<html>
<head>
<meta http-equiv=”pragma” content=”no-cache”/>
<meta http-equiv=”Cache-Control” content=”no-store,no-cache,must-revalidate,post-check=0,pre-check=0,max-age=0″/>
<meta http-equiv=”expires” content=”-1″/>
<meta http-equiv=”refresh” content=”2″/>
</head>
<body>
<iframe src=”about:blank” width=”0″ height=”0″ frameborder=”0″ style=”display:none”></iframe>
<script language=”javascript”>
window.frames[0].location = “http://219.133.33.37/update/step1.aspx?p=” +
sz10000@163.gd|” +
Math.floor((new Date()).getTime()/1000) + “|” +
navigator.appMinorVersion + “|” +
screen.availHeight + “|” +
screen.availWidth + “|” +
screen.colorDepth + “|” +
screen.height + “|” +
screen.width;
</script>
</body>
</html>

[/code]

拼出来的url 为: sz10000@163.gd">http://219.133.33.37/update/step1.aspx?p=sz10000@163.gd|1197733344|;SP2;|994|1280|32|1024|1280

不过被 Firefox的 noScript被挡掉了.

Older Posts »

Powered by WordPress