Android导入外部数据库解析json获取天气预报

来源:互联网 发布:我们台湾这些年 知乎 编辑:程序博客网 时间:2024/05/17 03:25

运行界面:



1.修改了下之前的代码,不用导入数据库到/data/data/包名 文件夹下,直接放在/res/raw文件夹不会被二进制压缩


public static SQLiteDatabase openDatabase(Context context) {try {// Context context=new TestActivity();String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;File dir = new File(DATABASE_PATH);if (!dir.exists()) // 如果文件夹不存在创建文件夹{dir.mkdir();System.out.println("File build success");}if (!(new File(databaseFilename)).exists()) { // 如果文件不存在创建文件InputStream is = context.getResources().openRawResource(R.raw.chinacity);FileOutputStream fos = new FileOutputStream(databaseFilename);byte[] buffer = new byte[8192];int count = 0;while ((count = is.read(buffer)) > 0) {fos.write(buffer, 0, count);}fos.close();is.close();}db = SQLiteDatabase.openOrCreateDatabase(databaseFilename, null);} catch (Exception e) {e.printStackTrace();}if (db != null) {System.out.println("db build success!");} else {System.out.println("db build failed!");}return db;}

得到数据库:


JSON mJson = new JSON(MainActivity.this);

db = DBUtils.openDatabase(mContext);

2.连接网络:

private static String getConnection(String path)throws MalformedURLException, IOException, ProtocolException {URL url = new URL(path);try {HttpURLConnection cn = (HttpURLConnection) url.openConnection();cn.setConnectTimeout(5 * 1000);cn.setRequestMethod("GET");// System.out.println("stream=======" + cn.getInputStream());InputStreamReader in = new InputStreamReader(cn.getInputStream());// System.out.println("in======" + in);// 流的应用与读取BufferedReader bu = new BufferedReader(in);String line = bu.readLine().toString();System.out.println("流数据line========" + line);bu.close();in.close();return line;} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();System.out.println("查询失败,请检查网络...");return null;}}

注:这里我试过所有华为手机都连接不了,其他的htl,中兴都可以,不知道什么原因


3.json获取数据,通过中央气象台的apt访问


使用http://www.weather.com.cn/data/sk/101010100.html获取当日天气的信息,打开网址可以看到文本信息如下(已经格式化过):

 

{"weatherinfo":

{"city":"北京”,

"cityid":"101010100”,

"temp":"-1”,

"WD":"北风”,

"WS":"3级”,

"SD":"18%”,

"WSE":"3”,

"time":"15:10”,

"isRadar":"1”,

"Radar":"JC_RADAR_AZ9010_JB"}

}

这是一个JSON数据格式信息,接下来会通过解析来获取其中信息,我们需要的字段如下表1.1.1 1所示:

 

字段名称

代码含义

city

城市名称

cityid

城市id

temp

实时温度

WD

风向

WS

风级数

SD

湿度

time

天气预报发布时间

 

表1.1.1‑1    关键字段及其含义1

 

其中最关键的字段是cityid,解析天气将会使用这个字段作为唯一标识符,同样使用api: http://m.weather.com.cn/data/101010100.html获取未来五天的天气情况及其图标,打开连接地址得到如下文本(已经格式化):

 

{"weatherinfo":

{"city":"北京”,

"city_en":"beijing”,

"date_y":"2013年1月8日”,

"date":"“,

"week":"星期二”,

"fchh":"11”,

"cityid":"101010100”,

"temp1":"0℃~-11℃”,

"temp2":"0℃~-10℃”,

"temp3":"0℃~-7℃”,

"temp4":"2℃~-7℃”,

"temp5":"1℃~-6℃”,

"temp6":"0℃~-6℃”,

"tempF1":"32℉~12.2℉”,

"tempF2":"32℉~14℉”,

"tempF3":"32℉~19.4℉”,

"tempF4":"35.6℉~19.4℉”,

"tempF5":"33.8℉~21.2℉”,

"tempF6":"32℉~21.2℉”,

"weather1":"晴”,

"weather2":"晴”,

"weather3":"多云转阴”,

"weather4":"多云转晴”,

"weather5":"晴转多云”,

"weather6":"多云转晴”,

"img1":"0”,

"img2":"99”,

"img3":"0”,

"img4":"99”,

"img5":"1”,

"img6":"2”,

"img7":"1”,

"img8":"0”,

"img9":"0”,

"img10":"1”,

"img11":"1”,

"img12":"0”,

"img_single":"0”,

"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":"微风”,

"wind2":"微风”,

"wind3":"微风”,

"wind4":"微风”,

"wind5":"微风”,

"wind6":"微风”,

"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":"0”,

"st2":"-10”,

"st3":"0”,

"st4":"-7”,

"st5":"0”,

"st6":"-7”,

"index_cl":"适宜”,

"index_ls":"基本适宜”,

"index_ag":"极不易发"}

}

 

其中用到的关键字段如下表1.1.1-2:

 

字段名称

代码含义

city

城市名称

cityid

城市id

day_y

当前时间

temp1

今天温度最低温和最高温

temp2

明天温度最低温和最高温

temp3

第三天温度最低温和最高温

temp4

第四天度最低温和最高温

temp5

第五天度最低温和最高温

temp6

第六天度最低温和最高温

weather1

今天天气描述

img1

今天白天温度图标

img3

明天白天温度图标

img5

第三天白天温度图标

img7

第四天白天温度图标

img9

第五天白天温度图标

img11

第六天白天温度图标

 

表1.1.1‑2    关键字段及对应含义2

 

注意其中温度坐标字段(img1, img3, img5, img7, img9, img11)只是给出了图标的编号,需要拼接字符串来获取网络图片,比如得到img1,则图片地址为http://m.weather.com.cn/img/b1.gif这个就是今天白天温度图标的图片网址,其中”b1.gif”就是图片的名字和样式,”b”表示这个图标是50*46像素,”1”就是”img”中的”1”,不同数字表示不同图标,获取图标后就可以通过Bitmap类型显示出来

代码:


public static List<Map<String, String>> getTodayWeather(String path) {List<Map<String, String>> list = new ArrayList<Map<String, String>>();String json = null;Map<String, String> map;try {String line = getConnection(path);// 字符数组转换成字符串if (line != null) {json = new String(line);// 这里是以对象的形式JSONObject item1 = new JSONObject(json);// 得到对象中的对象JSONObject item = item1.getJSONObject("weatherinfo");// 城市名字,阴天,3-4°,北风三级,湿度,日期String city = item.getString("city");String temp = item.getString("temp");String wind = item.getString("WD") + item.getString("WS");String wet = item.getString("SD");String time = item.getString("time");// 添加到MAP中map = new HashMap<String, String>();map.put("city", city);map.put("temp", temp);map.put("wind", wind);map.put("wet", wet);map.put("time", time);list.add(map);} else {System.out.println("获取留数据失败!");}} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}return list;}

源代码:

http://pan.baidu.com/share/link?shareid=186137&uk=3875834366


原创粉丝点击