看了郭霖大婶的《第一行代码》之后,收获颇丰。这里以书中的天气App作为最后的结尾。
功能需求:
1、可以罗列全国的省、市、县
2、可以查看全国任意城市的天气信息
3、可以自由切换城市,去查看其他城市的天气
4、提供手动更新以及后台自动更新天气的功能
接口:
1、获得省:http://guolin.tech/api/china(所有省),数据格式如下:
[{"id":1,"name":"北京"},{"id":2,"name":"上海"},{"id":3,"name":"天津"},{"id":4,"name":"重庆"},{"id":5,"name":"香港"},{"id":6,"name":"澳门"},{"id":7,"name":"台湾"},{"id":8,"name":"黑龙江"},{"id":9,"name":"吉林"},{"id":10,"name":"辽宁"},{"id":11,"name":"内蒙古"},{"id":12,"name":"河北"},{"id":13,"name":"河南"},{"id":14,"name":"山西"},{"id":15,"name":"山东"},{"id":16,"name":"江苏"},{"id":17,"name":"浙江"},{"id":18,"name":"福建"},{"id":19,"name":"江西"},{"id":20,"name":"安徽"},{"id":21,"name":"湖北"},{"id":22,"name":"湖南"},{"id":23,"name":"广东"},{"id":24,"name":"广西"},{"id":25,"name":"海南"},{"id":26,"name":"贵州"},{"id":27,"name":"云南"},{"id":28,"name":"四川"},{"id":29,"name":"西藏"},{"id":30,"name":"陕西"},{"id":31,"name":"宁夏"},{"id":32,"name":"甘肃"},{"id":33,"name":"青海"},{"id":34,"name":"新疆"}]
2、获得市:http://guolin.tech/api/china/28(四川省下面的市),数据格式如下:
[{"id":267,"name":"成都"},{"id":268,"name":"攀枝花"},{"id":269,"name":"自贡"},{"id":270,"name":"绵阳"},{"id":271,"name":"南充"},{"id":272,"name":"达州"},{"id":273,"name":"遂宁"},{"id":274,"name":"广安"},{"id":275,"name":"巴中"},{"id":276,"name":"泸州"},{"id":277,"name":"宜宾"},{"id":278,"name":"内江"},{"id":279,"name":"资阳"},{"id":280,"name":"乐山"},{"id":281,"name":"眉山"},{"id":282,"name":"凉山"},{"id":283,"name":"雅安"},{"id":284,"name":"甘孜"},{"id":285,"name":"阿坝"},{"id":286,"name":"德阳"},{"id":287,"name":"广元"}]
3、获得县:http://guolin.tech/api/china/28/267(成都市下面的县),数据格式如下:
[{"id":1967,"name":"成都","weather_id":"CN101270101"},{"id":1968,"name":"龙泉驿","weather_id":"CN101270102"},{"id":1969,"name":"新都","weather_id":"CN101270103"},{"id":1970,"name":"温江","weather_id":"CN101270104"},{"id":1971,"name":"金堂","weather_id":"CN101270105"},{"id":1972,"name":"双流","weather_id":"CN101270106"},{"id":1973,"name":"郫县","weather_id":"CN101270107"},{"id":1974,"name":"大邑","weather_id":"CN101270108"},{"id":1975,"name":"蒲江","weather_id":"CN101270109"},{"id":1976,"name":"新津","weather_id":"CN101270110"},{"id":1977,"name":"都江堰","weather_id":"CN101270111"},{"id":1978,"name":"彭州","weather_id":"CN101270112"},{"id":1979,"name":"邛崃","weather_id":"CN101270113"},{"id":1980,"name":"崇州","weather_id":"CN101270114"},{"id":1981,"name":"青白江区","weather_id":"CN101270115"}]
4、获得天气:http://guolin.tech/api/weather?cityid=CN101270101&key=9618c9b7080b4638a16fca8687bf9a60,数据格式如下:
{"HeWeather": [{"basic":{"cid":"CN101270101","location":"成都","parent_city":"成都","admin_area":"四川","cnty":"中国","lat":"30.65946198","lon":"104.06573486","tz":"+8.00","city":"成都","id":"CN101270101","update":{"loc":"2018-11-01 23:49","utc":"2018-11-01 15:49"}},"update":{"loc":"2018-11-01 23:49","utc":"2018-11-01 15:49"},"status":"ok","now":{"cloud":"0","cond_code":"100","cond_txt":"晴","fl":"12","hum":"95","pcpn":"0.0","pres":"1021","tmp":"12","vis":"1","wind_deg":"94","wind_dir":"东风","wind_sc":"1","wind_spd":"5","cond":{"code":"100","txt":"晴"}},"daily_forecast":[{"date":"2018-11-01","cond":{"txt_d":"晴"},"tmp":{"max":"23","min":"15"}},{"date":"2018-11-02","cond":{"txt_d":"晴"},"tmp":{"max":"22","min":"14"}},{"date":"2018-11-03","cond":{"txt_d":"晴"},"tmp":{"max":"20","min":"14"}}],"aqi":{"city":{"aqi":"99","pm25":"74","qlty":"良"}},"suggestion":{"comf":{"type":"comf","brf":"舒适","txt":"今天夜间不太热也不太冷,风力不大,相信您在这样的天气条件下,应会感到比较清爽和舒适。"},"sport":{"type":"sport","brf":"适宜","txt":"天气较好,赶快投身大自然参与户外运动,尽情感受运动的快乐吧。"},"cw":{"type":"cw","brf":"较适宜","txt":"较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"}}}]}
编码:
1、创建数据库和表
项目结构图db包用于存放数据库模型相关的代码,gson包用于存放GSON模型相关的代码,service包用于存放服务相关的代码,util包用于存放工具相关的代码。
添加项目依赖
implementation'org.litepal.android:core:1.6.1' --LitePal 用户对数据库进行操作
implementation'com.squareup.okhttp3:okhttp:3.10.0' --OkHttp用于进行网络请求
implementation'com.google.code.gson:gson:2.8.0' --GSON用于解析JSON数据
implementation'com.github.bumptech.glide:glide:3.7.0' Glide用于加载和展示图片
(1)、db包下分别建Province、City、County三个类(Lite中每一个实体类都必须继承DataSupport类,类中的字段与上午Json字符串对应),如下所示
public class Province extends DataSupport {
private int id;
private String provinceName;
private int provinceCode;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getProvinceCode() {
return provinceCode;
}
public void setProvinceCode(int provinceCode) {
this.provinceCode = provinceCode;
}
public String getProvinceName() {
return provinceName;
}
public void setProvinceName(String provinceName) {
this.provinceName = provinceName;
}
}
public class City extends DataSupport {
private int id;
private String cityName;
private int cityCode;
private int provinceId;
public int getCityCode() {
return cityCode;
}
public void setCityCode(int cityCode) {
this.cityCode = cityCode;
}
public int getProvinceId() {
return provinceId;
}
public void setProvinceId(int provinceId) {
this.provinceId = provinceId;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
public class County extends DataSupport {
private int id;
private String countyName;
private String weatherId;
private int cityId;
public int getCityId() {
return cityId;
}
public void setCityId(int cityId) {
this.cityId = cityId;
}
public String getWeatherId() {
return weatherId;
}
public void setWeatherId(String weatherId) {
this.weatherId = weatherId;
}
public String getCountyName() {
return countyName;
}
public void setCountyName(String countyName) {
this.countyName = countyName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
接下来在app/src/main目录下新建一个名为assets目录,然后在assets目录下新建一个litepal.xml文件,如下:
<?xml version="1.0" encoding="utf-8"?>
<litepal>
<dbname value="dean_weather" ></dbname> <!--数据库名称-->
<!--
版本号
-->
<version value="1" ></version>
<!--
映射实体类
<list>
<mapping class="com.test.model.Reader"></mapping>
<mapping class="com.test.model.Magazine"></mapping>
</list>
-->
<list>
<mapping class ="com.example.dean.deanweather.db.Province"></mapping>
<mapping class ="com.example.dean.deanweather.db.City"></mapping>
<mapping class ="com.example.dean.deanweather.db.County"></mapping>
</list>
</litepal>
最后在配置LitePalApplication,修改AndroidManifest.xml
AndroidManifest修改图
网友评论