http://flash.weather.com.cn/wmaps/xml/china.xml
能夠獲取國內各省及省會城市的天氣,可以通過pyName載入各省內城市的天氣
如:河北省 pyName="hebei"
http://flash.weather.com.cn/wmaps/xml/hebei.xml
就可以載入河北省各城市天氣,可以通過pyName="shijiazhuang"
載入石家莊的詳細天氣
http://flash.weather.com.cn/wmaps/xml/shijiazhuang.xml
"http://flash.weather.com.cn/wmaps/xml/"+place+".xml"
內容說明
state1="0" //天氣圖標1
state2="0" //天氣圖標2
stateDetailed="晴" //天氣說明
tem1="9" //最高氣溫
tem2="1" //最低氣溫
temNow="2" //當前實況氣溫
windState="南風轉北風小於3級" //風向風速預報
windDir="西北風" //當前實況風向
windPower="小於3級" //當前實況風速
humidity="77%" //溼度
time="09:55" //發佈時間
url="101090101" //鏈接地
有木有人能幫忙解析一下他的xml
在用Android獲取天氣預報數據時,大家一定會首先想到Google的天氣預報API,其實除了Google的天氣預報API,免費的天氣預報接口還有http://www.webservicex.net/globalweather.asmx?op=GetWeather、http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx和中央氣象臺的天氣預報,這三個是我最近試過的都可以訪問,網上其實還介紹的有www.ayandy.com,不過我沒試過 ^_^
現在就來談一談這幾個服務,google的就不說了,www.webservicex.net的這個是一個國外的,獲取中國國內的有點麻煩,且只有當天的天氣,所以果斷放棄...呵呵
而webservice.webxml.com.cn的這個確實不錯,訪問的數據是來自中國氣象局http://www.cma.gov.cn/ 數據的準確就不用說了,但他分爲付費和免費的,免費的服務有點不穩定,我就曾遇到一回,所以也不是很好,最後就只剩下中央氣象臺的天氣預報的API了,這個我不想說太多,雖然獲取時你首先要知道對應的城市碼,有點麻煩,其它的如穩定性與廣闊性也是很一流的,它可以精確到縣和區.下面就直奔主題:
這個服務的天氣預報的請求地址是:http://m.weather.com.cn/data/101070201.html,這個文本就是城市天氣URL,101070201代表的爲對應地區的編碼,執行URL,得到一個返回文本,是JSON格式的,如下(經過格式化):
- {
- "weatherinfo":{
- "city":"成都",
- "city_en":"chengdu",
- "date_y":"2011年11月30日",
- "date":"辛卯年",
- "week":"星期三",
- "fchh":"11", //預報發佈時間
- "cityid":"101270101",
- "temp1":"13℃~10℃",
- "temp2":"14℃~6℃",
- "temp3":"13℃~5℃",
- "temp4":"14℃~8℃",
- "temp5":"10℃~8℃",
- "temp6":"11℃~6℃",
- "tempF1":"55.4℉~50℉",
- "tempF2":"57.2℉~42.8℉",
- "tempF3":"55.4℉~41℉",
- "tempF4":"57.2℉~46.4℉",
- "tempF5":"50℉~46.4℉",
- "tempF6":"51.8℉~42.8℉",
- "weather1":"陰轉多雲",
- "weather2":"多雲轉晴",
- "weather3":"多雲轉陰",
- "weather4":"陣雨",
- "weather5":"陣雨轉小雨",
- "weather6":"小雨轉陰",
- "img1":"2",
- "img2":"1",
- "img3":"1",
- "img4":"0",
- "img5":"1",
- "img6":"2",
- "img7":"3",
- "img8":"99",
- "img9":"3",
- "img10":"7",
- "img11":"7",
- "img12":"2",
- "img_single":"2",
- "img_title1":"陰",
- "img_title2":"多雲",
- "img_title3":"多雲",
- "img_title4":"晴",
- "img_title5":"多雲",
- "img_title6":"陰",
- "img_title7":"陣雨",
- "img_title8":"陣雨",
- "img_title9":"陣雨",
- "img_title10":"小雨",
- "img_title11":"小雨",
- "img_title12":"陰",
- "img_title_single":"陰",
- "wind1":"北風小於3級",
- "wind2":"北風小於3級",
- "wind3":"北風小於3級",
- "wind4":"南風轉北風小於3級",
- "wind5":"北風小於3級",
- "wind6":"北風小於3級",
- "fx1":"北風",
- "fx2":"北風",
- "fl1":"小於3級",
- "fl2":"小於3級",
- "fl3":"小於3級",
- "fl4":"小於3級",
- "fl5":"小於3級",
- "fl6":"小於3級",
- "index":"舒適",
- "index_d":"建議着薄型套裝或牛仔衫褲等春秋過渡裝。年老體弱者宜着套裝、夾克衫等。",
- "index48":"舒適",
- "index48_d":"建議着薄型套裝或牛仔衫褲等春秋過渡裝。年老體弱者宜着套裝、夾克衫等。",
- "index_uv":"最弱",
- "index48_uv":"弱",
- "index_xc":"適宜",
- "index_tr":"很適宜",
- "index_co":"較舒適",
- "st1":"14",
- "st2":"10",
- "st3":"14",
- "st4":"6",
- "st5":"13",
- "st6":"4",
- "index_cl":"較適宜",
- "index_ls":"不太適宜",
- "index_ag":"較易發"
- }
- }
- {
- "weatherinfo":{
- "city":"成都",
- "city_en":"chengdu",
- "date_y":"2011年11月30日",
- "date":"辛卯年",
- "week":"星期三",
- "fchh":"11", //預報發佈時間
- "cityid":"101270101",
- "temp1":"13℃~10℃",
- "temp2":"14℃~6℃",
- "temp3":"13℃~5℃",
- "temp4":"14℃~8℃",
- "temp5":"10℃~8℃",
- "temp6":"11℃~6℃",
- "tempF1":"55.4℉~50℉",
- "tempF2":"57.2℉~42.8℉",
- "tempF3":"55.4℉~41℉",
- "tempF4":"57.2℉~46.4℉",
- "tempF5":"50℉~46.4℉",
- "tempF6":"51.8℉~42.8℉",
- "weather1":"陰轉多雲",
- "weather2":"多雲轉晴",
- "weather3":"多雲轉陰",
- "weather4":"陣雨",
- "weather5":"陣雨轉小雨",
- "weather6":"小雨轉陰",
- "img1":"2",
- "img2":"1",
- "img3":"1",
- "img4":"0",
- "img5":"1",
- "img6":"2",
- "img7":"3",
- "img8":"99",
- "img9":"3",
- "img10":"7",
- "img11":"7",
- "img12":"2",
- "img_single":"2",
- "img_title1":"陰",
- "img_title2":"多雲",
- "img_title3":"多雲",
- "img_title4":"晴",
- "img_title5":"多雲",
- "img_title6":"陰",
- "img_title7":"陣雨",
- "img_title8":"陣雨",
- "img_title9":"陣雨",
- "img_title10":"小雨",
- "img_title11":"小雨",
- "img_title12":"陰",
- "img_title_single":"陰",
- "wind1":"北風小於3級",
- "wind2":"北風小於3級",
- "wind3":"北風小於3級",
- "wind4":"南風轉北風小於3級",
- "wind5":"北風小於3級",
- "wind6":"北風小於3級",
- "fx1":"北風",
- "fx2":"北風",
- "fl1":"小於3級",
- "fl2":"小於3級",
- "fl3":"小於3級",
- "fl4":"小於3級",
- "fl5":"小於3級",
- "fl6":"小於3級",
- "index":"舒適",
- "index_d":"建議着薄型套裝或牛仔衫褲等春秋過渡裝。年老體弱者宜着套裝、夾克衫等。",
- "index48":"舒適",
- "index48_d":"建議着薄型套裝或牛仔衫褲等春秋過渡裝。年老體弱者宜着套裝、夾克衫等。",
- "index_uv":"最弱",
- "index48_uv":"弱",
- "index_xc":"適宜",
- "index_tr":"很適宜",
- "index_co":"較舒適",
- "st1":"14",
- "st2":"10",
- "st3":"14",
- "st4":"6",
- "st5":"13",
- "st6":"4",
- "index_cl":"較適宜",
- "index_ls":"不太適宜",
- "index_ag":"較易發"
- }
- }
訪問http://m.weather.com.cn/data5/city.xml得到一級列表(省、直轄市、自治區),結果用逗號隔開,id和城市名稱使用豎線“|”隔開;結果示例如下:
- 01|北京,02|上海,03|天津,04|重慶,05|黑龍江,06|吉林,07|遼寧,08|內蒙古,09|河北,10|山西,11|陝西,.....
- 01|北京,02|上海,03|天津,04|重慶,05|黑龍江,06|吉林,07|遼寧,08|內蒙古,09|河北,10|山西,11|陝西,.....
然後通過這一次訪問得到的一級碼得到二級碼,如查找河北的:http://m.weather.com.cn/data5/city09.xml在city後加上一級碼得到:
- 0901|石家莊,0902|保定,0903|張家口,0904|承德,0905|唐山,0906|廊坊,0907|滄州,0908|衡水,0909|邢臺,0910|邯鄲,0911|秦皇島
- 0901|石家莊,0902|保定,0903|張家口,0904|承德,0905|唐山,0906|廊坊,0907|滄州,0908|衡水,0909|邢臺,0910|邯鄲,0911|秦皇島
然後再通過得到的二級碼得到三級的縣或區的編碼:如唐山:http://m.weather.com.cn/data5/city0905.xml:
- 090501|唐山,090502|豐南,090503|豐潤,090504|灤縣,090505|灤南,090506|樂亭,090507|遷西,090508|玉田,090509|唐海,090510|遵化,090511|遷安
- 090501|唐山,090502|豐南,090503|豐潤,090504|灤縣,090505|灤南,090506|樂亭,090507|遷西,090508|玉田,090509|唐海,090510|遵化,090511|遷安
- 090507|101090507
- 090507|101090507
則可知河北.唐山.遷西的城市編碼爲:101090507,得到它的天氣則通過訪問http://m.weather.com.cn/data/101090507.html得到。
好了,相信通過上面的敘述,大家應該知道怎麼得到一個城市的天氣了;但接下來問題又來了,那我們怎樣通過Android訪問來得到全國任意一個地區的天氣呢?難道每一次訪問網絡三次得到城市碼,再用它訪問m.weather.com.cn這個網址得到天氣碼?天啊?這怎麼行!要知道我們手中的移動設備可不像電腦那樣"富有",流量是要扣錢的!!
相信計算機算法好的同鞋已經想到了,就是通過程序將有的城市對應的天氣碼一次訪問到本地用android上的數據庫保存起來,這樣在每一次需要時,只需查詢一下數據庫就可以了,那關鍵就是怎樣遍歷得到所有的城市碼,現給出本人的遍歷代碼(來自於本人天氣預報系統):
- //一個自定義的網絡訪問工具類
- WebAccessTools webTools = new WebAccessTools(this); //這裏的this爲context對象
- //得到訪問網絡的內容
- String webContent=webTools.getWebContent("http://m.weather.com.cn/data5/city.xml");
- //第一次解析得到的爲省份或一級直轄市
- String[][] provinces = WeaterInfoParser.parseCity(webContent); //WeatherInfoParser爲自定義的一個解析字符串類
- String[] groups = new String[provinces.length];
- String[][] childs = new String[provinces.length][];
- String[][] cityCode = new String[provinces.length][];
- for(int i=0; i< provinces.length; i++) {
- groups[i] = provinces[i][1];
- //由省份碼來得到城市碼
- StringBuffer urlBuilder= new StringBuffer("http://m.weather.com.cn/data5/city");
- urlBuilder.append(provinces[i][0]);
- urlBuilder.append(".xml");
- webContent = webTools.getWebContent(urlBuilder.toString());
- String[][] citys = WeaterInfoParser.parseCity(webContent);
- //用於保存所的有二級市對應的towns縣區
- String[][][] towns = new String[citys.length][][];
- //計算總的城鎮數
- int sum=0;
- for(int j=0; j<citys.length; j++) {
- //由城市碼來得到地方碼
- urlBuilder= new StringBuffer("http://m.weather.com.cn/data5/city");
- urlBuilder.append(citys[j][0]);
- urlBuilder.append(".xml");
- webContent = webTools.getWebContent(urlBuilder.toString());
- towns[j] = WeaterInfoParser.parseCity(webContent);
- sum = sum + towns[j].length;
- }
- childs[i] = new String[sum];
- cityCode[i] = new String[sum];
- sum=0;
- for(int j=0; j<citys.length; j++) {
- for(int n=0; n<towns[j].length; n++) {
- if(n==0)
- childs[i][sum] = towns[j][n][1];
- else
- childs[i][sum] = towns[j][0][1] + "." + towns[j][n][1];
- urlBuilder= new StringBuffer("http://m.weather.com.cn/data5/city");
- urlBuilder.append(towns[j][n][0]);
- urlBuilder.append(".xml");
- webContent = webTools.getWebContent(urlBuilder.toString());
- String[][] code=WeaterInfoParser.parseCity(webContent);
- cityCode[i][sum] = code[0][1];
- sum = sum + 1;
- }
- }
- urlBuilder=null;
- }
- //=======這裏得到的groups數組記錄的是得到的34個一級地區字符串 =====================
- //=======childs記錄的是與groups數組對應的一級地區對應的市級別或縣區的字符串名,如:上海.閔行的形式(上海市沒有.爲上海)===
- //=======cityCode和childs形式相同,只不過記錄的爲地區的城市碼=====================
- //=======下面就是將得到的城市碼保存到數據庫中,這裏建了二張表,省份表和城市表,城市表通過province_id與省份表關聯===========
- //============================Create Database================================
- //打開或創建一個數據庫,數據庫路徑爲:/data/data/包名/數據庫文件
- String path="/data"+ Environment.getDataDirectory().getAbsolutePath() + "/com.weather.app/db_weather.db";
- SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase
- (path, null);
- //創建一個省份表
- String sql="create table provinces (_id integer primary key autoincrement, name text)";
- database.execSQL(sql);
- //創建城市表
- sql = "create table citys (_id integer primary key autoincrement, province_id integer, name text, city_num text)";
- database.execSQL(sql);
- //插入省份數據
- ContentValues cv = null;
- for(int i=0; i<provinces.length; i++) {
- cv = new ContentValues();
- cv.put("name", provinces[i][1]);
- database.insert("provinces", null, cv);
- }
- //插入城市數據
- for(int i=0; i<childs.length; i++) {
- for(int j=0; j<childs[i].length; j++) {
- cv = new ContentValues();
- cv.put("province_id", i);
- cv.put("name", childs[i][j]);
- cv.put("city_num", cityCode[i][j]);
- database.insert("citys", null, cv);
- }
- }
- cv = null;
- database.close();
- //一個自定義的網絡訪問工具類
- WebAccessTools webTools = new WebAccessTools(this); //這裏的this爲context對象
- //得到訪問網絡的內容
- String webContent=webTools.getWebContent("http://m.weather.com.cn/data5/city.xml");
- //第一次解析得到的爲省份或一級直轄市
- String[][] provinces = WeaterInfoParser.parseCity(webContent); //WeatherInfoParser爲自定義的一個解析字符串類
- String[] groups = new String[provinces.length];
- String[][] childs = new String[provinces.length][];
- String[][] cityCode = new String[provinces.length][];
- for(int i=0; i< provinces.length; i++) {
- groups[i] = provinces[i][1];
- //由省份碼來得到城市碼
- StringBuffer urlBuilder= new StringBuffer("http://m.weather.com.cn/data5/city");
- urlBuilder.append(provinces[i][0]);
- urlBuilder.append(".xml");
- webContent = webTools.getWebContent(urlBuilder.toString());
- String[][] citys = WeaterInfoParser.parseCity(webContent);
- //用於保存所的有二級市對應的towns縣區
- String[][][] towns = new String[citys.length][][];
- //計算總的城鎮數
- int sum=0;
- for(int j=0; j<citys.length; j++) {
- //由城市碼來得到地方碼
- urlBuilder= new StringBuffer("http://m.weather.com.cn/data5/city");
- urlBuilder.append(citys[j][0]);
- urlBuilder.append(".xml");
- webContent = webTools.getWebContent(urlBuilder.toString());
- towns[j] = WeaterInfoParser.parseCity(webContent);
- sum = sum + towns[j].length;
- }
- childs[i] = new String[sum];
- cityCode[i] = new String[sum];
- sum=0;
- for(int j=0; j<citys.length; j++) {
- for(int n=0; n<towns[j].length; n++) {
- if(n==0)
- childs[i][sum] = towns[j][n][1];
- else
- childs[i][sum] = towns[j][0][1] + "." + towns[j][n][1];
- urlBuilder= new StringBuffer("http://m.weather.com.cn/data5/city");
- urlBuilder.append(towns[j][n][0]);
- urlBuilder.append(".xml");
- webContent = webTools.getWebContent(urlBuilder.toString());
- String[][] code=WeaterInfoParser.parseCity(webContent);
- cityCode[i][sum] = code[0][1];
- sum = sum + 1;
- }
- }
- urlBuilder=null;
- }
- //=======這裏得到的groups數組記錄的是得到的34個一級地區字符串 =====================
- //=======childs記錄的是與groups數組對應的一級地區對應的市級別或縣區的字符串名,如:上海.閔行的形式(上海市沒有.爲上海)===
- //=======cityCode和childs形式相同,只不過記錄的爲地區的城市碼=====================
- //=======下面就是將得到的城市碼保存到數據庫中,這裏建了二張表,省份表和城市表,城市表通過province_id與省份表關聯===========
- //============================Create Database================================
- //打開或創建一個數據庫,數據庫路徑爲:/data/data/包名/數據庫文件
- String path="/data"+ Environment.getDataDirectory().getAbsolutePath() + "/com.weather.app/db_weather.db";
- SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase
- (path, null);
- //創建一個省份表
- String sql="create table provinces (_id integer primary key autoincrement, name text)";
- database.execSQL(sql);
- //創建城市表
- sql = "create table citys (_id integer primary key autoincrement, province_id integer, name text, city_num text)";
- database.execSQL(sql);
- //插入省份數據
- ContentValues cv = null;
- for(int i=0; i<provinces.length; i++) {
- cv = new ContentValues();
- cv.put("name", provinces[i][1]);
- database.insert("provinces", null, cv);
- }
- //插入城市數據
- for(int i=0; i<childs.length; i++) {
- for(int j=0; j<childs[i].length; j++) {
- cv = new ContentValues();
- cv.put("province_id", i);
- cv.put("name", childs[i][j]);
- cv.put("city_num", cityCode[i][j]);
- database.insert("citys", null, cv);
- }
- }
- cv = null;
- database.close();
- public class WebAccessTools {
- /**
- * 當前的Context上下文對象
- */
- private Context context;
- /**
- * 構造一個網站訪問工具類
- * @param context 記錄當前Activity中的Context上下文對象
- */
- public WebAccessTools(Context context) {
- this.context = context;
- }
- /**
- * 根據給定的url地址訪問網絡,得到響應內容(這裏爲GET方式訪問)
- * @param url 指定的url地址
- * @return web服務器響應的內容,爲<code>String</code>類型,當訪問失敗時,返回爲null
- */
- public String getWebContent(String url) {
- //創建一個http請求對象
- HttpGet request = new HttpGet(url);
- //創建HttpParams以用來設置HTTP參數
- HttpParams params=new BasicHttpParams();
- //設置連接超時或響應超時
- HttpConnectionParams.setConnectionTimeout(params, 3000);
- HttpConnectionParams.setSoTimeout(params, 5000);
- //創建一個網絡訪問處理對象
- HttpClient httpClient = new DefaultHttpClient(params);
- try{
- //執行請求參數項
- HttpResponse response = httpClient.execute(request);
- //判斷是否請求成功
- if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
- //獲得響應信息
- String content = EntityUtils.toString(response.getEntity());
- return content;
- } else {
- //網連接失敗,使用Toast顯示提示信息
- Toast.makeText(context, "網絡訪問失敗,請檢查您機器的聯網設備!", Toast.LENGTH_LONG).show();
- }
- }catch(Exception e) {
- e.printStackTrace();
- } finally {
- //釋放網絡連接資源
- httpClient.getConnectionManager().shutdown();
- }
- return null;
- }
- }
- public class WebAccessTools {
- /**
- * 當前的Context上下文對象
- */
- private Context context;
- /**
- * 構造一個網站訪問工具類
- * @param context 記錄當前Activity中的Context上下文對象
- */
- public WebAccessTools(Context context) {
- this.context = context;
- }
- /**
- * 根據給定的url地址訪問網絡,得到響應內容(這裏爲GET方式訪問)
- * @param url 指定的url地址
- * @return web服務器響應的內容,爲<code>String</code>類型,當訪問失敗時,返回爲null
- */
- public String getWebContent(String url) {
- //創建一個http請求對象
- HttpGet request = new HttpGet(url);
- //創建HttpParams以用來設置HTTP參數
- HttpParams params=new BasicHttpParams();
- //設置連接超時或響應超時
- HttpConnectionParams.setConnectionTimeout(params, 3000);
- HttpConnectionParams.setSoTimeout(params, 5000);
- //創建一個網絡訪問處理對象
- HttpClient httpClient = new DefaultHttpClient(params);
- try{
- //執行請求參數項
- HttpResponse response = httpClient.execute(request);
- //判斷是否請求成功
- if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
- //獲得響應信息
- String content = EntityUtils.toString(response.getEntity());
- return content;
- } else {
- //網連接失敗,使用Toast顯示提示信息
- Toast.makeText(context, "網絡訪問失敗,請檢查您機器的聯網設備!", Toast.LENGTH_LONG).show();
- }
- }catch(Exception e) {
- e.printStackTrace();
- } finally {
- //釋放網絡連接資源
- httpClient.getConnectionManager().shutdown();
- }
- return null;
- }
- }
下面的解析工具類也只一個方法:
- public class WeaterInfoParser {
- /**
- * 通過解析content,得到一個一維爲城市編號,二維爲城市名的二維數組
- * 解析的字符串的形式爲: <code>編號|城市名,編號|城市名,.....</code>
- * @param content 需要解析的字符串
- * @return 封裝有城市編碼與名稱的二維數組
- */
- public static String[][] parseCity(String content) {
- //判斷content不爲空
- if(content!=null&&content.trim().length()!=0) {
- StringTokenizer st=new StringTokenizer(content, ",");
- int count = st.countTokens();
- String[][] citys = new String[count][2];
- int i=0, index=0;
- while(st.hasMoreTokens()) {
- String city = st.nextToken();
- index = city.indexOf('|');
- citys[i][0] = city.substring(0, index);
- citys[i][1] = city.substring(index+1);
- i = i+1;
- }
- return citys;
- }
- return null;
- }
- }
- public class WeaterInfoParser {
- /**
- * 通過解析content,得到一個一維爲城市編號,二維爲城市名的二維數組
- * 解析的字符串的形式爲: <code>編號|城市名,編號|城市名,.....</code>
- * @param content 需要解析的字符串
- * @return 封裝有城市編碼與名稱的二維數組
- */
- public static String[][] parseCity(String content) {
- //判斷content不爲空
- if(content!=null&&content.trim().length()!=0) {
- StringTokenizer st=new StringTokenizer(content, ",");
- int count = st.countTokens();
- String[][] citys = new String[count][2];
- int i=0, index=0;
- while(st.hasMoreTokens()) {
- String city = st.nextToken();
- index = city.indexOf('|');
- citys[i][0] = city.substring(0, index);
- citys[i][1] = city.substring(index+1);
- i = i+1;
- }
- return citys;
- }
- return null;
- }
- }
如上,這樣就得到了國內所有地區的城市碼數據庫文件了,上面的方法在模擬器中運行時很慢,數分鐘才能出來結果,要有耐心^_^!!
其通過上面的代碼我們得到最重要的是db_weather.db這個文件,在Android中我們只要得到了它就可在程序第一次運行時直接導入到/data/data/包名/databases目錄中就行了,已後直接調用,
關於數據庫的導入,實質上就是文件的複製,我們只需在將數據庫文件放在程序包中的res/raw目錄中在運行時複製到databases目錄是就行了,下面同樣給出導入代碼:
- //將res/raw中的城市數據庫導入到安裝的程序中的database目錄下
- public void importInitDatabase() {
- //數據庫的目錄
- String dirPath="/data/data/com.weather.app/databases";
- File dir = new File(dirPath);
- if(!dir.exists()) {
- dir.mkdir();
- }
- //數據庫文件
- File dbfile = new File(dir, "db_weather.db");
- try {
- if(!dbfile.exists()) {
- dbfile.createNewFile();
- }
- //加載欲導入的數據庫
- InputStream is = this.getApplicationContext().getResources().openRawResource(R.raw.db_weather);
- FileOutputStream fos = new FileOutputStream(dbfile);
- byte[] buffere=new byte[is.available()];
- is.read(buffere);
- fos.write(buffere);
- is.close();
- fos.close();
- }catch(FileNotFoundException e){
- e.printStackTrace();
- }catch(IOException e) {
- e.printStackTrace();
- }
- }
- //將res/raw中的城市數據庫導入到安裝的程序中的database目錄下
- public void importInitDatabase() {
- //數據庫的目錄
- String dirPath="/data/data/com.weather.app/databases";
- File dir = new File(dirPath);
- if(!dir.exists()) {
- dir.mkdir();
- }
- //數據庫文件
- File dbfile = new File(dir, "db_weather.db");
- try {
- if(!dbfile.exists()) {
- dbfile.createNewFile();
- }
- //加載欲導入的數據庫
- InputStream is = this.getApplicationContext().getResources().openRawResource(R.raw.db_weather);
- FileOutputStream fos = new FileOutputStream(dbfile);
- byte[] buffere=new byte[is.available()];
- is.read(buffere);
- fos.write(buffere);
- is.close();
- fos.close();
- }catch(FileNotFoundException e){
- e.printStackTrace();
- }catch(IOException e) {
- e.printStackTrace();
- }
- }
我得到的城市碼數據庫文件可到http://download.csdn.net/detail/xianqiang1/3896880下載
上面的是我從天氣預報系統中截取的部分代碼,如有錯誤,歡迎留言指出!!
參照文章:使用JACKSON解析JSON(HttpClient 3處理請求)http://sarin.iteye.com/blog/821534
聲明:感謝summerxzg和kpiao,指出了數據庫文件的錯誤:我使用sqlitebrowser工具查看了一下我生成的db文件,顯示出的的城市碼數爲2566個而summerxzg指出的爲2583條,我的爲不全。kpiao指出的citys表中的province_id與provinces表中的id不對應是由於代碼中的是citys表創建時province_id是由0開始,而provinces表中的自動增長是從1開始故存在不匹配問題。爲了準確起見,大家還是自己遍歷一個數據庫,小子我由於近期較忙暫時不會對上面內容作更改{^_^},再次感謝你們!