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年11月21日

根据sitemap.org的schema使用JAXB生成sitemap.xml的读写方法

Filed under: Java,Web — 标签:, , , , , , — lizongbo @ 13:37

根据sitemap.org的schema使用JAXB生成sitemap.xml的读写方法

sitemap协议的中文说明在:
http://sitemaps.org/zh_CN/protocol.php
对应的schema在:
Sitemap 的 XML 格式定义: http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd
Sitemap 索引文件格式定义:http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd
向google提交sitemap.xml是在:
https://www.google.com/webmasters/tools/home?hl=zh_CN
向百度提交sitemap.xml是在:http://sitemap.baidu.com/

xsd文件生成java代码的操作步骤如下:
1.下载xsd文件保存到D:\dev\xsds。
2.在JDK的bin目录下运行xjc命令生成java文件:
D:\dev\Java\jdk1.6.0_22\bin>xjc -d D:\dev\javasrc -p com.lizongbo.web.sitemaps.sitemap D:\dev\xsds\sitemap.xsd
parsing a schema…
compiling a schema…
com\lizongbo\web\sitemaps\sitemap\ObjectFactory.java
com\lizongbo\web\sitemaps\sitemap\TChangeFreq.java
com\lizongbo\web\sitemaps\sitemap\TUrl.java
com\lizongbo\web\sitemaps\sitemap\Urlset.java
com\lizongbo\web\sitemaps\sitemap\package-info.java

D:\dev\Java\jdk1.6.0_22\bin>xjc -d D:\dev\javasrc -p com.lizongbo.web.sitemaps.siteindex D:\dev\xsds\siteindex.xsd
parsing a schema…
compiling a schema…
com\lizongbo\web\sitemaps\siteindex\ObjectFactory.java
com\lizongbo\web\sitemaps\siteindex\Sitemapindex.java
com\lizongbo\web\sitemaps\siteindex\TSitemap.java
com\lizongbo\web\sitemaps\siteindex\package-info.java
3.将java文件都复制到java工程的src目录的对应包中。
4.将xsd文件复制到java工程的com.lizongbo.web.sitemaps包对应的文件夹中。
5.封装工具类SitemapUtil.java,代码如下:
[code]
package com.lizongbo.web.sitemaps;

import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.xml.sax.SAXException;

import com.lizongbo.web.sitemaps.siteindex.Sitemapindex;
import com.lizongbo.web.sitemaps.siteindex.TSitemap;
import com.lizongbo.web.sitemaps.sitemap.TUrl;
import com.lizongbo.web.sitemaps.sitemap.Urlset;
import com.lizongbo.web.sitemaps.sitemap.ObjectFactory;

/**
* 通过jaxb对sitemap.xml进行生成和解析的工具类
*
* @author lizongbo
*
*/
public class SitemapUtil {
private final static ObjectFactory sitemapFac = new ObjectFactory();
private final static com.lizongbo.web.sitemaps.siteindex.ObjectFactory siteindexFac = new com.lizongbo.web.sitemaps.siteindex.ObjectFactory();
private final static String SITEMAP_SCHEMA_LOCATION = “http://www.sitemaps.org/schemas/sitemap/0.9
+ “http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd”;
private final static String SITEINDEX_SCHEMA_LOCATION = “http://www.sitemaps.org/schemas/sitemap/0.9
+ “http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd”;
private final static String DOCTYPE = “<?xml version=\”1.0\” encoding=\”utf-8\”?>”;
private final static String ProcessingInstruction = “<?xml-stylesheet type=’text/xsl’ href=’http://618119.com/wp-content/plugins/google-sitemap-generator/sitemap.xsl’ ?>”;

public static Sitemapindex createSitemapindex() {
return siteindexFac.createSitemapindex();
}

public static TSitemap createTSitemap() {
return siteindexFac.createTSitemap();
}

public static TUrl createTUrl() {
return sitemapFac.createTUrl();
}

public static Urlset createUrlset() {
return sitemapFac.createUrlset();
}

public static String getSitemapXML(Urlset urlset) throws JAXBException,
SAXException {
JAXBContext jc = JAXBContext
.newInstance(“com.lizongbo.web.sitemaps.sitemap”);
Marshaller m = jc.createMarshaller();
StringWriter sw = new StringWriter();
sw.append(DOCTYPE).append(“\n”);
sw.append(ProcessingInstruction).append(“\n”);
//设置xml文本编码
m.setProperty(Marshaller.JAXB_ENCODING, “UTF-8”);
//设置为格式化输出结果
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
//设置为只输出xml节点片段
m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
//指定xsd路径
m.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, SITEMAP_SCHEMA_LOCATION);
SchemaFactory sf = SchemaFactory
.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(SitemapUtil.class
.getResource(“sitemap.xsd”));
//绑定Schema进行校验
m.setSchema(schema);
m.marshal(urlset, sw);
return sw.toString();
}

public static Urlset getUrlset(String xml) throws JAXBException,
SAXException {
JAXBContext jc = JAXBContext
.newInstance(“com.lizongbo.web.sitemaps.sitemap”);
Unmarshaller um = jc.createUnmarshaller();
SchemaFactory sf = SchemaFactory
.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(SitemapUtil.class
.getResource(“sitemap.xsd”));
um.setSchema(schema);
return (Urlset) um.unmarshal(new StringReader(xml));
}

public static String getSitemapindexXML(Sitemapindex sitemapindex)
throws JAXBException, SAXException {
JAXBContext jc = JAXBContext
.newInstance(“com.lizongbo.web.sitemaps.siteindex”);
Marshaller m = jc.createMarshaller();
StringWriter sw = new StringWriter();
sw.append(DOCTYPE).append(“\n”);
sw.append(ProcessingInstruction).append(“\n”);
m.setProperty(Marshaller.JAXB_ENCODING, “UTF-8”);
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
m.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
SITEINDEX_SCHEMA_LOCATION);
SchemaFactory sf = SchemaFactory
.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(SitemapUtil.class
.getResource(“siteindex.xsd”));
m.setSchema(schema);
m.marshal(sitemapindex, sw);
return sw.toString();
}

public static Sitemapindex getSitemapindex(String xml)
throws JAXBException, SAXException {
JAXBContext jc = JAXBContext
.newInstance(“com.lizongbo.web.sitemaps.siteindex”);
Unmarshaller um = jc.createUnmarshaller();
SchemaFactory sf = SchemaFactory
.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(SitemapUtil.class
.getResource(“siteindex.xsd”));
um.setSchema(schema);
return (Sitemapindex) um.unmarshal(new StringReader(xml));
}

public static void main(String[] args) throws JAXBException, SAXException {
Urlset urlset = SitemapUtil.createUrlset();
TUrl url = SitemapUtil.createTUrl();
url.setLoc(“http://lizongbo.com”);
urlset.getUrl().add(url);
String xml = getSitemapXML(urlset);
System.out.println(xml);
xml = xml.replace(“lizongbo”, “google”);
System.out.println(getSitemapXML(getUrlset(xml)));
Sitemapindex sitemapindex = SitemapUtil.createSitemapindex();
TSitemap tSitemap = SitemapUtil.createTSitemap();
tSitemap.setLoc(“http://618119.com/sitemap.xml”);
sitemapindex.getSitemap().add(tSitemap);
xml = getSitemapindexXML(sitemapindex);
System.out.println(xml);
xml = xml.replace(“lizongbo”, “google”);
System.out.println(getSitemapindexXML(getSitemapindex(xml)));
}
}

[/code]

2010年04月14日

将google托管的ajax js文件同步到本地服务器

Filed under: Java — 标签:, , — lizongbo @ 21:14
google提供了供全世界访问的公共javascript库托管,
参考:http://code.google.com/intl/zh-CN/apis/ajaxlibs/
只要使用 <script src=”http://www.google.com/jsapi”></script> 引用google的js,
然后使用 google.load() 方法,就可以指定载入对应库的对应版本。 例如:google.load(“jquery”, “1”);
就可以载入当前最新的jquery库。
虽然google承诺无限期地托管各版本的js文件,但是由于大部分人知道的原因,在国内使用这种方式共享js资源存在一定风险。
因此,我参考google对这些js的部署结构,将js下载回来放到了本地服务器。
目前托管的js库有以下组件:
jQuery
jQuery UI
Prototype
script.aculo.us
MooTools
Dojo
SWFObject
Yahoo! 用户界面库 (YUI)
Ext Core
全部都同步下来,操作步骤如下:
先在Firefox里打开:
http://www.google.com/jsapi
然后将js保存到成本地文件,然后用Aptana打开,并格式化,然后找到
google.loader.rpl,然后把括号里一段json对象复制出来,定义成  var jslibs;
然后通过js生成对应js的url。然后使用java下载到本地保存到对应目录。
代码参考:
<SCRIPT LANGUAGE=”JavaScript”>
<!–
var jslibs={
“:jquery” : {
“versions” : {
“:1.2.3” : {
“uncompressed” : “jquery.js”,
“compressed” : “jquery.min.js”
},
“:1.3.1” : {
“uncompressed” : “jquery.js”,
“compressed” : “jquery.min.js”
},
“:1.3.0” : {
“uncompressed” : “jquery.js”,
“compressed” : “jquery.min.js”
},
“:1.3.2” : {
“uncompressed” : “jquery.js”,
“compressed” : “jquery.min.js”
},
“:1.2.6” : {
“uncompressed” : “jquery.js”,
“compressed” : “jquery.min.js”
},
“:1.4.0” : {
“uncompressed” : “jquery.js”,
“compressed” : “jquery.min.js”
},
“:1.4.1” : {
“uncompressed” : “jquery.js”,
“compressed” : “jquery.min.js”
},
“:1.4.2” : {
“uncompressed” : “jquery.js”,
“compressed” : “jquery.min.js”
}
},
“aliases” : {
“:1” : “1.4.2”,
“:1.4” : “1.4.2”,
“:1.3” : “1.3.2”,
“:1.2” : “1.2.6”
}
}
}
//–>
</SCRIPT>
<TEXTAREA id=”urls” NAME=”urls” ROWS=”40″ COLS=”80″></TEXTAREA>
<SCRIPT LANGUAGE=”JavaScript”>
<!–
var urls=””;
for(var jslibName in jslibs){
var jslibList=jslibs[jslibName];
for(var jslibver in jslibList[“versions”]){
var url1=”http://ajax.googleapis.com/ajax/libs/”+jslibName.replace(“:”,””)+”/”+jslibver.replace(“:”,””)+”/”+jslibList[“versions”][jslibver][“uncompressed“];
var url2=”http://ajax.googleapis.com/ajax/libs/”+jslibName.replace(“:”,””)+”/”+jslibver.replace(“:”,””)+”/”+jslibList[“versions”][jslibver][“compressed“];
urls=urls+”\n”+url1+”\n”+url2;
}
}
document.getElementById(“urls”).value=”String[] urlsStrings={”
+urls
+”};”
+”  for (int i = 0; i < urlsStrings.length; i++) {”
+”   try {”
+”    URL url = new URL(urlsStrings[i]);”
+”    File file = new File(\”E:/js\”, url.getPath());”
+”    file.getParentFile().mkdirs();”
+”    PrintWriter pw = new PrintWriter(file);”
+”    URLConnection urlc = url.openConnection();”
+”    urlc.setConnectTimeout(5000);”
+”    urlc.connect();”
+”    BufferedReader in = new BufferedReader(new InputStreamReader(”
+”      urlc.getInputStream(), \”UTF-8\”));”
+”    String line;”
+”    while ((line = in.readLine()) != null) {”
+”     pw.write(line);”
+”     // sb.append(line).append(‘\n’);”
+”    }”
+”    pw.close();”
+”    in.close();”
+”   } catch (Exception e) {”
+”    e.printStackTrace();”
+”   }”;
//–>
</SCRIPT>

Powered by WordPress