“Java”目录存档

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

2011年05月5日,星期四

从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.;

import .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..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("_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是把我用手机测试定位的时候就把数据采集分析好了,确实强大。

Tags: , , , , ,

获取Android的Java源代码并在Eclipse中关联查看的最新方法

2011年01月1日,星期六

在Google搜索“Android Eclipse 源代码”得到的结果并不合适,因为Google的Eclipse Android开发插件已经更新到了ADT 8.0.1.相应的lib关联java源代码的方式也变了。
以下是获取Android的Java源代码并在Eclipse中关联的最新操作方法和步骤:
1.首先下载Android的SDK安装文件:http://dl.google.com/android/android-sdk_r08-windows.zip
然后解压到:E:\Java\android\android-sdk-windows,运行目录中的SDK Manager.exe,下载好Android不同版本的API包。

2.然后下载或在线安装Eclipse插件ADT,
安装包地址为:http://dl.google.com/android/ADT-8.0.1.zip
在线安装地址:https://dl-ssl.google.com/android/eclipse/
来源:ttp://developer.android.com/sdk/eclipse-adt.html

2.ADT插件安装好之后,下载我已经打包好的Android 2.3(gingerbread)的java源代码压缩包:
下载地址:http://lizongbo.googlecode.com/files/android_gingerbread_javasrc.zip
来源:http://code.google.com/p/lizongbo/downloads/list
下载后复制到E:\Java\android\javasrc。

3.Android的Java源代码压缩包下载好之后,在Eclipse中设置好SDK配置:
操作步骤为:主菜单->Window–>Preferences–>选择Android,SDK Location设置为:E:\Java\android\android-sdk-windows,然后点右下角的Apply来生效。

4.开始进行关联Android的源代码,在Eclipse中新建一个Android工程,选中工程,主菜单 –>Project–>Properties–>选择Android,Project Build Target选中Android 2.3。
然后再点左边的“Java Build Path”,切换到 Libraries 面板,展开 Android2.3 这个lib,点击android.jar的Source attachment,再点Edit.在对话框中点“External File”,选择E:/Java/android/javasrc/android_gingerbread_javasrc.zip,点OK进行确认。这样就把Android的java代码关联好了。

5.在同一个工程中,选中工程,主菜单–>Project–>Properties–>选择 Android,Project Build Target分别选择Android1.5,Android1.6,Android2.01,Android2.1- update1,Android2.2,按上面第四步的操作重复设置关联。
以后在任何Android工程里按住Ctrl用鼠标点击Java类名都可以浏览到对应的java源代码。
Android各个版本对应的源代码为:
Android1.5对应android_cupcake_javasrc.zip
Android1.6对应android_donut_javasrc.zip
Android2.01和Android2.1-update1对应的是android_eclair_javasrc.zip
Android2.2对应android_froyo_javasrc.zip
Android2.3对应android_gingerbread_javasrc.zip

6.在Eclipse配置好源代码关联之后之后的信息会存储在:
E:\lizongbo\workspacenew\.metadata\.plugins\org.eclipse.core.resources\.root\.indexes\properties.index
E:\lizongbo\workspacenew\.metadata\.plugins\org.eclipse.jdt.core\variablesAndContainers.dat

同时记录Android的java源代码压缩包制作方法如下:
1.先在远程的Ubuntu Linux服务器(是Linode VPS)上安装以下组件(JDK1.6已经安装好了):
sudo apt-get install git-core gnupg flex bison gperf libsdl-dev libesd0-dev libwxgtk2.6-dev build-essential zip curl libncurses5-dev zlib1g-dev
信息来自Android官方:http://source.android.com/source/download.html
2.运行 sudo apt-get install lib32readline5-dev
3.安装Repo脚本:
我懒得配PATH了,于是直接安装到了/usr/local/bin。
root@lizongbo.com:~/bin# cd /usr/local/bin
root@lizongbo.com:/usr/local/bin# ls
geoip-lookup
root@lizongbo.com:/usr/local/bin# curl http://android.git.kernel.org/repo >/usr/local/bin/repo
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
Dload  Upload   Total   Spent    Left  Speed
100 17412  100 17412    0     0  27517      0 –:–:– –:–:– –:–:– 31148
root@lizongbo.com:/usr/local/bin# chmod a+x /usr/local/bin/repo
注意在下载该脚本的时候,可能会遇到服务器出错,下载的内容不对,运行命令的时候会提示错误“syntax error near unexpected token `newline’”。
root@lizongbo.com:/usr/local/bin# repo init -u git://android.git.kernel.org/platform/manifest.git -b cupcake
/usr/local/bin/repo: line 1: syntax error near unexpected token `newline’
/usr/local/bin/repo: line 1: `<!DOCTYPE HTML PUBLIC “-//IETF//DTD HTML 2.0//EN”>’
root@lizongbo.com:/usr/local/bin# more repo
<!DOCTYPE HTML PUBLIC “-//IETF//DTD HTML 2.0//EN”>
<html><head>
<title>500 Internal Server Error</title>
</head><body>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error or
misconfiguration and was unable to complete
your request.</p>
<p>Please contact the server administrator,
webmaster@kernel.org and inform them of the time the error occurred,
and anything you might have done that may have
caused the error.</p>
<p>More information about this error may be available
in the server error log.</p>
</body></html>
从内容看是服务器出错了,因此需要重新下载脚本.
4.建立mydroid目录,按代码分支下载对应版本的Android完整的源代码,比如下载Android2.3的代码:
root@lizongbo.com:~/mydroid# mkdir gingerbread
root@lizongbo.com:~/mydroid# cd gingerbread/
root@lizongbo.com:~/mydroid/gingerbread# repo init -u git://android.git.kernel.org/platform/manifest.git -b gingerbread
root@lizongbo.com:~/mydroid/gingerbread# repo sync
Android1.5的代码分支:
root@lizongbo.com:~/mydroid/cupcake# repo init -u git://android.git.kernel.org/platform/manifest.git -b cupcake
root@lizongbo.com:~/mydroid/cupcake# repo sync
Android1.6的代码分支:
root@lizongbo.com:~/mydroid/donut# repo init -u git://android.git.kernel.org/platform/manifest.git -b donut
root@lizongbo.com:~/mydroid/donut# repo sync
Android2.0.1和2.1的代码分支:
root@lizongbo.com:~/mydroid/eclair# repo init -u git://android.git.kernel.org/platform/manifest.git -b eclair
root@lizongbo.com:~/mydroid/eclair# repo sync
Android2.2的代码分支:
root@lizongbo.com:~/mydroid/froyo# repo init -u git://android.git.kernel.org/platform/manifest.git -b froyo
root@lizongbo.com:~/mydroid/froyo# repo sync

如果遇到代码下载速度太慢,可以指定hosts,hosts配置不住为:
root@lizongbo.com:~/mydroid/gingerbread/.repo/projects# nslookup
> android.git.kernel.org
Server:         72.14.188.5
Address:        72.14.188.5#53
Non-authoritative answer:
android.git.kernel.org  canonical name = android.git..kernel.org.
android.git.geo.kernel.org      canonical name = android.git.us.kernel.org.
Name:   android.git.us.kernel.org
Address: 149.20.20.141
Name:   android.git.us.kernel.org
Address: 204.152.191.45
> exit

root@lizongbo.com:/etc# vi hosts
204.152.191.45 android.git.us.kernel.org
204.152.191.45 android.git.kernel.org

网上有文章介绍使用提取java文件并打包的pytho脚本可以生成源代码包,参考:

http://android.opensourceror.org/2010/01/18/android-source/

http://blog.michael-forster.de/2008/12/view-android-source-code-in-eclipse.html
Python下载地址:http://www.python.org/ftp/python/2.6.6/python-2.6.6.msi
但是在Android2.2和Android2.3的代码里,Android写一了一些mock的类,比如android.webkit.WebView,使用python脚本打包的话,会把mock的源代码打进去,而不是真正的源代码,
因此我根据python脚本写了个java程序来提取java文件过滤冗余的java代码并打包。
生成的java源代码压缩:android_cupcake_javasrc.zip,android_donut_javasrc.zip,android_eclair_javasrc.zip,android_froyo_javasrc.zip,android_gingerbread_javasrc.zip
目前在code..com上传了android_gingerbread_javasrc.zip,欢迎下载。

以下是各版本的Android java 下载地址:

Android2.3: http://lizongbo.googlecode.com/files/android_gingerbread_javasrc.zip

Android1.5: http://lizongbo.googlecode.com/files/android_cupcake_javasrc.zip
Android1.6: http://lizongbo.googlecode.com/files/android_donut_javasrc.zip
Android2.1: http://lizongbo.googlecode.com/files/android_eclair_javasrc.zip
Android2.2: http://lizongbo.googlecode.com/files/android_froyo_javasrc.zip

新年快乐!

Tags: , , , , , , , ,

手机上常见的安装文件名后缀和mime类型

2010年12月29日,星期三

手机上常见安装文件名后缀和mime类型
kjava是最通用的平台,有些手机只支持kjava,j2me的安装文件名以jar和jad结尾。
Android平台的软件后缀名是apk
例如手机QQ1.0(Android)Beta3Build0077的安装文件是:MobileQQ1.0(Android)Beta3Build0077.apk
如果安装了Java虚拟机的话,也可以安装文件名以jar和jad结尾的软件。
塞班(Symbian)的S60V3和S60v5平台支持的文件名后缀有:sis,,jar,,wgz。
例如:2010(S60V3)Beta4Build1186的安装文件名为:QQ2010(S60V3)Beta4Build1186.sisx
塞班(Symbian)S40平台只支持kjava因此安装文件名也是以jar和jad结尾。
塞班(Symbian)S60V2平台通常也支持kjava,也支持sis格式的安装文件,因此支持的格式一共有sis,jar,jad。
Symbian3是塞班新出的平台支持的文件名后缀有jar,jad,sisx和sis。
黑莓(BlackBerry)平台支持的文件名后缀有jar,jad,cod。
Plam webOS支持的安装文件名后缀有jar,jad,prc,ipk。
MTK平台的手机分支持java和不支持java两种情况,一般支持的安装文件名后缀有jar,jad,
斯凯平台是MTK平台上的中间件,支持的文件名后缀为:mrp
iPhone操作系统支持的按成程序文件名后缀为:pxl,ipa,deb。
Windows Mobile平台支持的文件名后缀有cab,jar,jad,exe。
例如手机QQ2008(Pocket PC)正式版 Build0093的安装文件名为:PPCQQ2008_Build093.CAB
下面是各种文件名后缀在nginx的mime.types中的配置:
text/vnd.sun.j2me.app-descriptor      jad;
application/java-archive              jar war ear;
application/x-java-archive-diff       jardiff;
application/vnd.android.package-archive apk;
application/vnd.ms-cab-compressed              cab;
application/octet-stream              bin exe dll;
application/vnd.symbian.install       sis;
x-epoc/x-sisx-app                     sisx;
application/iphone                    pxl ipa;
application/vnd.palm                  prc pdb;
application/vnd..ipk             ipk;
application/vnd.rim.cod               cod;
application/mrp                       mrp;
x-nokia-widget                        wgz;
application/octet-stream              deb;

下面的一些mime是关于图片和视频的,列在这里备忘:
video/3gpp                            3gpp 3gp;
image/jpeg                            jpeg jpg;
image/png                             png;
image/gif                             gif;

相关参考链接:http://nds.nokia.com/uaprof/NN81-1r100.xml
APK文件格式说明:http://en.wikipedia.org/wiki/APK_%28file_format%29
sis文件格式:http://zh.wikipedia.org/zh/SIS_%28%E6%96%87%E4%BB%B6%E6%A0%BC%E5%BC%8F%29
jad文件格式: http://en.wikipedia.org/wiki/JAD_%28file_format%29
手机QQ下载:http://msoft.qq.com/d/mqq/

Tags: , , , , , , , , , , ,