参照《第一行代码》开发CoolWeather (一)
来源:互联网 发布:win10 禁用windows 键 编辑:程序博客网 时间:2024/04/30 10:06
CoolWeather 开发总结(一)
先看看开发完成后的界面吧
大体上和书上代码类似 但是由于书上所用的天气接口已经被关闭,所以换了一个和风天气的接口 并且重新设计了一下界面
**
1.功能需求及技术可行性分析
**
1)功能需求
1. 可以罗列出全国所有的省、市、县。 2. 可以查看全国任意城市的天气信息。 3. 可以自由地切换城市,去查看其他城市的天气。 4. 提供手动更新以及后台自动更新天气的功能。
2)技术可行性分析
1.如何获取全国的省市县信息以及如何获取这些区域的天气信息
这里省市县的信息依然用的是中国天气网的接口 因为它返回的数据比较简单
省份接口是
http://www.weather.com.cn/data/list3/city.xml
城市接口是:
http://www.weather.com.cn/data/list3/city省份代号.xml
县级接口是:
http://www.weather.com.cn/data/list3/city省份代号城市代号.xml
注意如果直接用浏览器打开上面的网址显示的是
这里是猜测是因为浏览器解析xml出现了错误,我们可以写个小demo测试一下
URL url = new URL("http://www.weather.com.cn/data/list3/city.xml"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); InputStream is = conn.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is,"UTF-8")); String line = ""; while((line=br.readLine())!=null){ System.out.println(line); }
那么可以看到解析出来的数据就是这种”代号|名称“的格式 只要将数据解析出来放listview显示出来就可以了
这样省市县的数据就已经解决了
再看天气信息 书上写的接口
http://www.weather.com.cn/data/cityinfo/天气代号.html
这个接口已经关闭无法使用了 我这里用的是和风天气接口 只要去官网注册一下就可以免费使用它的接口
https://free-api.heweather.com/v5/weather?city=城市名称/城市代号&key=你认证的key
注意城市名称如果是中文的话应该转码成utf-8 当然拼音格式的就不用转码了
返回的数据是json格式的
比较复杂 我们稍微排列一下就容易明白了
我只用了basic里的当前更新时间 daily_forecast更新的未来七天的天气 now当前气温
suggestion里的提示信息
根据code代号可以通过相应的接口获取对于天气的图片
2.创建数据库和表
1.首先在项目下建包 将相应功能的类分开管理
activity包用于存放所有活动相关的代码,db包用于存放所有数据库相关的代码,model包用于存放所有模型相关的代码,receiver包用于存放所有广播接收器相关的代码,service包用于存放所有服务相关的代码,util包用于存放所有工具相关的代码
1.在db包下新建CoolWeatherOpenHelper类 通过该类在数据库生成省份、城市、县级三张表
public class CoolWeatherOpenHelper extends SQLiteOpenHelper { /** * Province建表语句 */ private static final String CREATE_PROVINCE = "create table Province (" + "id integer primary key autoincrement," + "province_code text," + "province_name text)"; /** * City建表语句 */ private static final String CREATE_CITY = "create table City(" + "id integer primary key autoincrement," + "city_code text," + "city_name text," + "province_id integer)"; /** * County建表语句 */ private static final String CREATE_COUNTY = "create table county(" + "id integer primary key autoincrement," + "county_code text," + "county_name text," + "city_id integer)"; public CoolWeatherOpenHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase db) { //创建三张表 db.execSQL(CREATE_PROVINCE); db.execSQL(CREATE_CITY); db.execSQL(CREATE_COUNTY); } @Override public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { }} ```
2.然后再分别建立省份、城市和县所对应的实体类
- 接着我们还需要创建一个CoolWeatherDB类,把一些常用的数据库操作封装起来
首先私有化构造方法 单例模式
然后分别写了从数据库读取存放省份城市和县的方法
/** * 数据库类 * @author Administrator * 封装数据库操作 */ public class CoolWeatherDB { /** * 数据库名称 */ private static final String DB_NAME = "cool_weather"; /** * 数据库版本 */ private static final int VERSION = 1; private static CoolWeatherDB coolWeatherDB; private SQLiteDatabase db; /** * 构造方法私有化 * @param context */ private CoolWeatherDB(Context context) { CoolWeatherOpenHelper dbHelper = new CoolWeatherOpenHelper(context, DB_NAME, null, VERSION); db = dbHelper.getWritableDatabase(); } /** * 获得coolWeatherDB实例 * @param context 上下文环境 * @return */ public synchronized static CoolWeatherDB getInstance(Context context){ if(coolWeatherDB==null){ coolWeatherDB = new CoolWeatherDB(context); } return coolWeatherDB; } /** * 将Province保存到数据库 * @param province */ public void saveProvince(Province province){ if(province!=null){ ContentValues values = new ContentValues(); values.put("province_name", province.getProvinceName()); values.put("province_code", province.getProvinceCode()); db.insert("Province", null, values); } } /** * 从数据库中获得全国的省份信息 * @return */ public List<Province> loadProvinces(){ List<Province> list = new ArrayList<Province>(); Cursor cursor = db.query("Province", null, null, null, null, null, null); if(cursor.moveToFirst()){ do{ Province province = new Province(); province.setId(cursor.getInt(cursor.getColumnIndex("id"))); province.setProvinceCode(cursor.getString(cursor.getColumnIndex("province_code"))); province.setProvinceName(cursor.getString(cursor.getColumnIndex("province_name"))); list.add(province); }while(cursor.moveToNext()); } return list; } /** * 将城市信息保存到数据库 * @param city */ public void saveCity(City city){ if(city!=null){ ContentValues values = new ContentValues(); values.put("city_code", city.getCityCode()); values.put("city_name", city.getCityName()); values.put("province_id", city.getProvinceId()); db.insert("City", null, values); } } /** * 从数据库获得城市信息 * @return */ public List<City> loadCities(int provinceId){ List<City> list = new ArrayList<City>(); Cursor cursor = db.query("City", null, "province_id=?", new String[]{String.valueOf(provinceId)}, null, null, null); if(cursor.moveToFirst()){ do{ City city = new City(); city.setId(cursor.getInt(cursor.getColumnIndex("id"))); city.setCityCode(cursor.getString(cursor.getColumnIndex("city_code"))); city.setCityName(cursor.getString(cursor.getColumnIndex("city_name"))); city.setProvinceId(provinceId); list.add(city); }while(cursor.moveToNext()); } return list; } /** * 将县级信息保存到数据库 * @param county */ public void saveCounty(County county){ if(county!=null){ ContentValues values = new ContentValues(); values.put("county_code", county.getCountyCode()); values.put("county_name", county.getCountyName()); values.put("city_id", county.getCityId()); db.insert("County", null, values); } } /** * 从数据库中加载县级信息 * @param cityId * @return */ public List<County> loadCounties(int cityId){ List<County> list = new ArrayList<County>(); Cursor cursor = db.query("County", null, "city_id=?", new String[]{String.valueOf(cityId)}, null, null, null); if(cursor.moveToFirst()){ do{ County county = new County(); county.setId(cursor.getInt(cursor.getColumnIndex("id"))); county.setCountyCode(cursor.getString(cursor.getColumnIndex("county_code"))); county.setCountyName(cursor.getString(cursor.getColumnIndex("county_name"))); county.setCityId(cityId); list.add(county); }while(cursor.moveToNext()); } return list; }}
这样第一阶段的代码就写好了
- 参照《第一行代码》开发CoolWeather (一)
- CoolWeather天气个人学习笔记1(第一行代码实战)
- CoolWeather(《第一行代码》实战)遍历全国各省市县总结
- 第一行代码的最后coolweather的项目实战
- 第一行代码的最后coolweather的项目实战
- 【android】第一行代码CoolWeather中遇到的问题
- 第一行代码酷欧天气开发(一)
- 第一行代码,UI开发部分<一>
- (第一行代码)笔记(一)
- 第一行代码复读收获(一)
- 第一行代码学习笔记(一)
- 第一行代码Android学习(一)
- <第一行代码>笔记一
- 《第一行代码》复习一
- 《第一行代码(第二版)》的学习(一)
- 《第一行代码-Android》学习笔记(一)
- 第一行代码Android学习笔记(一)activity
- 《第一行代码》第七章 读取系统联系人(一)
- ImageLoader的简易配置
- mt7620a上带机量的提高(三)
- strchr函数的用法
- 深入浅出JMS(二)--ActiveMQ简单介绍以及安装
- 09上机练习2
- 参照《第一行代码》开发CoolWeather (一)
- 计算机视觉(opencv和卷积)
- java中级面试题总结(1)
- API 25 (Android 7.1.1 API) view.SurfaceView
- 开源集锦(三)GitHub开源控件(一)
- 一个简单的取就近的五分钟的整数倍的日期
- 使用Docker部署Mesos
- 【C语言】称硬币问题
- js随时判断网络状态