空空天气预报
Posted fankongkong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了空空天气预报相关的知识,希望对你有一定的参考价值。
之前写了一款基于android的空空天气预报,能够查看全国各个省、市、县的未来三天的天气预报,有穿衣指数和运动指数建议;
最近准备找工作了,现在重新回顾一下思路。
主要用到的知识有解析xml技术,解析json,mvc架构。
1、首先看下界面结果;
2、代码思路
2-1 代码架构图
2-2 设计思路
2-2-1 首先解析一个省市县xml文件,里面的代码样式如图:
<province id="01" name="北京"> <city id="0101" name="北京"> <county id="010101" name="北京" weatherCode="101010100"/> <county id="010102" name="海淀" weatherCode="101010200"/> <county id="010103" name="朝阳" weatherCode="101010300"/> <county id="010104" name="顺义" weatherCode="101010400"/> <county id="010105" name="怀柔" weatherCode="101010500"/> <county id="010106" name="通州" weatherCode="101010600"/> <county id="010107" name="昌平" weatherCode="101010700"/> <county id="010108" name="延庆" weatherCode="101010800"/> <county id="010109" name="丰台" weatherCode="101010900"/> <county id="010110" name="石景山" weatherCode="101011000"/> <county id="010111" name="大兴" weatherCode="101011100"/> <county id="010112" name="房山" weatherCode="101011200"/> <county id="010113" name="密云" weatherCode="101011300"/> <county id="010114" name="门头沟" weatherCode="101011400"/> <county id="010115" name="平谷" weatherCode="101011500"/> </city> </province>
2-2-2 模型层的建立
主要是在model层里面建立City、County、Province三个类,下面贴一部分代码:
public class County { private int id;//县城主id private String countyName;//县城名 private String countyCode;//县城代码 private int cityId;//城市id ... public class City { private int id;//城市id private String cityName;//城市名称 private String cityCode;//城市代码 private int provinceId;//省id ... public class Province { private int id;//省id private String provinceName;//省名 private String provinceCode;//省代码 public int getId() { ...
2-2-3 数据库的设计
设计两个类,一个类是CoolWeatherOpenHelper 继承自 SQLiteOpenHelper,用于数据库的创建和更新,大致代码如下:
public class CoolWeatherOpenHelper extends SQLiteOpenHelper { public CoolWeatherOpenHelper(Context context, String name,CursorFactory factory, int version) { super(context, name, factory, version); } //创建省、市、县的sql语句 public static final String CREATE_PROVINCE = "create table Province (" + "id integer primary key autoincrement," + "provinceName text," + "provinceCode text)"; public static final String CREATE_CITY = "create table City (" + "id integer primary key autoincrement," + "cityName text," + "cityCode text," + "provinceId integer)"; public static final String CREATE_COUNTY = "create table County (" + "id integer primary key autoincrement," + "countyName text," + "countyCode text," + "cityId integer)"; //创建数据库 @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_PROVINCE); //create tables db.execSQL(CREATE_CITY); db.execSQL(CREATE_COUNTY); } ...
设计第二个类CoolWeatherDB,用于插入xml文件中省市县的天气预报代码
1 public class CoolWeatherDB { 2 /** 3 * database name 数据库名 4 */ 5 public static final String DB_NAME = "cool_weather.db"; 6 /** 7 * database version ,database is used for operating the database (insert , update, delete, select) 8 */ 9 public static final int VERSION = 1; 10 private static CoolWeatherDB coolWeatherDB; 11 private SQLiteDatabase db; 12 13 /** 14 * constructor构造函数 15 */ 16 private CoolWeatherDB(Context context) { 17 CoolWeatherOpenHelper dbHelper = new CoolWeatherOpenHelper(context, DB_NAME, null, VERSION); 18 db = dbHelper.getWritableDatabase();//call onCreate method 19 } 20 /** 21 * get the instance of CoolWeatherDB, use the singleInstance modern 22 * 获得CoolWeatherDB对象,这里使用了单例模式 23 */ 24 public synchronized static CoolWeatherDB getInstance(Context context) { 25 if(coolWeatherDB == null) 26 coolWeatherDB = new CoolWeatherDB(context); 27 return coolWeatherDB; 28 } 29 /** 30 * insert province information to the database 31 * 插入省信息到数据库中 32 */ 33 public void saveProvince(Province province) { 34 if(province != null) { 35 ContentValues values = new ContentValues(); 36 values.put("provinceName", province.getProvinceName()); 37 values.put("provinceCode", province.getProvinceCode()); 38 db.insert("Province", null, values); 39 } 40 } 41 /** 42 * insert a list of provinces 43 * 插入一系列省信息到数据库中 44 * @param provinces 45 */ 46 public void saveMangProvinces(ArrayList<Province> provinces) { 47 if(provinces.size() > 0) { 48 for(Province province: provinces) { 49 ContentValues values = new ContentValues(); 50 values.put("provinceName", province.getProvinceName()); 51 values.put("provinceCode", province.getProvinceCode()); 52 db.insert("Province", null, values); 53 } 54 } 55 } 56 /** 57 * save city 58 * 插入城市信息到数据库中 59 */ 60 public void saveCity(City city) { 61 if(city != null) { 62 ContentValues values = new ContentValues(); 63 values.put("cityName", city.getCityName()); 64 values.put("cityCode", city.getCityCode()); 65 values.put("provinceId", city.getProvinceId()); 66 db.insert("City", null, values); 67 } 68 } 69 /** 70 * insert a list of cities 71 * 插入一系列的城市信息到数据库中 72 * @param cities 73 */ 74 public void saveMangCities(ArrayList<City> cities) { 75 76 if(cities.size() > 0) { 77 for(City city: cities) { 78 ContentValues values = new ContentValues(); 79 values.put("cityName", city.getCityName()); 80 values.put("cityCode", city.getCityCode()); 81 values.put("provinceId", city.getProvinceId()); 82 db.insert("City", null, values); 83 } 84 } 85 } 86 /** 87 * save county 88 * 插入县信息到数据库中 89 */ 90 public void saveCounty(County county) { 91 if(county != null) { 92 ContentValues values = new ContentValues(); 93 values.put("countyName", county.getCountyName()); 94 values.put("countyCode", county.getCountyCode()); 95 values.put("cityId", county.getCityId()); 96 db.insert("County", null, values); 97 } 98 } 99 /** 100 * 插入一系列县信息到数据库中 101 * @param counties 102 */ 103 public void saveMangCounties(ArrayList<County> counties) { 104 if(counties.size() > 0) { 105 for(County county: counties) { 106 ContentValues values = new ContentValues(); 107 values.put("countyName", county.getCountyName()); 108 values.put("countyCode", county.getCountyCode()); 109 values.put("cityId", county.getCityId()); 110 db.insert("County", null, values); 111 } 112 } 113 } 114 /** 115 * get province infor from the db 116 * 从数据库中获取省信息 117 */ 118 public List<Province> loadProvinces() { 119 List<Province> list = new ArrayList<Province>(); 120 Cursor cursor = db.query("Province", null, null, null, null, null, null); 121 if(cursor.moveToFirst()) { 122 do{ 123 Province province = new Province(); 124 province.setId(cursor.getInt(cursor.getColumnIndex("id"))); 125 province.setProvinceName(cursor.getString(cursor.getColumnIndex("provinceName"))); 126 province.setProvinceCode(cursor.getString(cursor.getColumnIndex("provinceCode"))); 127 list.add(province); 128 }while(cursor.moveToNext()); 129 } 130 return list; 131 } 132 /** 133 * get city infor from the db by provinceId 134 * 根据省id从数据库中获取城市信息 135 */ 136 public List<City> loadCities(int provinceId) { 137 List<City> list = new ArrayList<City>(); 138 Cursor cursor = db.query("City", null, "provinceId=?", new String[] {String.valueOf(provinceId)}, null, null, null); 139 if(cursor.moveToFirst()) { 140 do{ 141 City city= new City(); 142 city.setId(cursor.getInt(cursor.getColumnIndex("id"))); 143 city.setCityName(cursor.getString(cursor.getColumnIndex("cityName"))); 144 city.setCityCode(cursor.getString(cursor.getColumnIndex("cityCode"))); 145 list.add(city); 146 }while(cursor.moveToNext()); 147 } 148 return list; 149 } 150 /** 151 * get county information from the database by cityId 152 * 根据城市id从数据库中获取县城信息 153 */ 154 public List<County> loadCounties(String cityCode) { 155 List<County> list = new ArrayList<County>(); 156 Cursor cursor = db.query("County", null, "cityId=?", new String[]{String.valueOf(cityCode)}, null, null, null); 157 if(cursor.moveToFirst()) { 158 do{ 159 County county = new County(); 160 county.setId(cursor.getInt(cursor.getColumnIndex("id"))); 161 county.setCountyName(cursor.getString(cursor.getColumnIndex("countyName"))); 162 county.setCountyCode(cursor.getString(cursor.getColumnIndex("countyCode"))); 163 county.setCityId(Integer.parseInt(cityCode)); 164 list.add(county); 165 }while(cursor.moveToNext()); 166 } 167 return list; 168 } 169 }
2-2-4 设计解析xml工具类ParseCity
1 //用于解析xml中的省市县及对应的代码 2 public class ParseCity extends Thread { 3 public static void parse(final InputStream in,final CoolWeatherDB coolWeatherDB) { 4 new Thread(new Runnable() { 5 ArrayList<Province> provinces = new ArrayList<Province>();// ArrayList存储多个省 6 ArrayList<City> cities = new ArrayList<City>();// ArrayList存储多个市 7 ArrayList<County> counties = new ArrayList<County>();// ArrayList存储多个县 8 @Override 9 public void run() { 10 try { 11 // 用XmlPullParserFactory去解析xml文件,获取实例 12 XmlPullParserFactory factory = XmlPullParserFactory 13 .newInstance(); 14 // 获取XmlPullParser类型的实例 15 XmlPullParser xmlPullParser = factory.newPullParser(); 16 // 设置输入流和编码类型 17 xmlPullParser.setInput(in, "utf-8");// set stream 18 // 获取事件的类型 19 int eventType = xmlPullParser.getEventType(); 20 // 获得的是事件类型,常用的有:XmlPullParser.END_DOCUMENT(xml文档结束) 21 // XmlPullParser.START_DOCUMENT(文档开始),XmlPullParser.START_TAG(开始标签) 22 // XmlPullParser.END_TAG(结束标签).XmlPullParser.TEXT(内容) 23 String cityid = "";// 城市的id 24 String fid = "";//设置外键的值 25 // 使用while循环,解析完整个xml文件 26 while (eventType != XmlPullParser.END_DOCUMENT) { 27 String nodeName = xmlPullParser.getName(); 28 switch (eventType) { 29 // begin to parse somenode 开始解析某个结点 30 case XmlPullParser.START_DOCUMENT: 31 break; 32 case XmlPullParser.START_TAG: { 33 //获取xml中的标记名称,有Province、City、County这三种 34 String tagName = xmlPullParser.getName(); 35 //如果是省的话 36 if ("province".equals(tagName)) { 37 // get the count of province 38 int count = xmlPullParser.getAttributeCount(); 39 String provinceId = null;//省id 40 String provinceName = null;//省名称 41 for (int i = 0; i < count; i++) { 42 // 获取id 43 String attrName = xmlPullParser.getAttributeName(i); 44 // 获取省名称 北京 45 String attrValue = xmlPullParser.getAttributeValue(i); 46 if ("id".equals(attrName)) { 47 // 用于设置在城市中的外键 48 fid = attrValue; 49 provinceId = attrValue; 50 } else if ("name".equals(attrName)) { 51 provinceName = attrValue; 52 } 53 } 54 Province province = new Province(); 55 province.setProvinceCode(provinceId); 56 province.setProvinceName(provinceName); 57 // put the parsed data into the list provinces 58 //保存省信息到数据库中 59 coolWeatherDB.saveProvince(province); 60 } 61 //如果是城市 62 if ("city".equals(tagName)) { 63 //获取城市的数量 64 int count = xmlPullParser.getAttributeCount(); 65 //城市名称 66 String cityName = null; 67 //城市代码 68 String cityCode = null; 69 //城市的外键,省id 70 int provinceId = Integer.parseInt(fid); 71 for (int i = 0; i < count; i++) { 72 String attrName = xmlPullParser.getAttributeName(i); 73 String attrValue = xmlPullParser 74 .getAttributeValue(i); 75 if ("id".equals(attrName)) { 76 cityid = attrValue;// set foreign id in 77 cityCode = attrValue;//城市代码 78 } else if ("name".equals(attrName)) { 79 cityName = attrValue;//城市名称 80 } 81 } 82 City city = new City(); 83 city.setCityCode(cityCode); 84 city.setCityName(cityName); 85 city.setProvinceId(provinceId); 86 //在数据库中保存城市 87 coolWeatherDB.saveCity(city); 88 // cities.add(city); 89 } 90 if ("county".equals(nodeName)) { 91 int count = xmlPullParser.getAttributeCount(); 以上是关于空空天气预报的主要内容,如果未能解决你的问题,请参考以下文章片段中的 Asynctask 未到达 onPostExecute
Azure 机器人微软Azure Bot 编辑器系列 : 机器人/用户提问回答模式,机器人从API获取响应并组织答案 (The Bot Framework Composer tutorial(代码片段