码迷,mamicode.com
首页 > 其他好文 > 详细

自己实现简单的天气预报应用(2)

时间:2015-03-04 08:31:26      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

 

2.设计模型层

从上一篇中获得的天气预报情况类似这样:

        {
            "temp_low": "7",    //最低温度
            "cityno": "chengdu",    
            "wind": "北风",   //风向
            "citynm": "成都", //城市/地区
            "weaid": "265", 
            "winpid": "125",    //风力id
            "windid": "20", //风向id
            "weatid1": "8",
            "humi_high": "0",   //最高湿度
            "temperature": "12℃\/7℃",//最高温度和最低温度
            "cityid": "101270101",
            "temp_high": "12",  //最高温度
            "humidity": "0℉\/0℉",   //湿度范围
            "weather_icon": "http:\/\/api.k780.com:88\/upload\/weather\/d\/2.gif",
            "days": "2015-03-03",//日期
            "weatid": "3",
            "humi_low": "0",    //最低湿度
            "weather": "阴转小雨",  //天气
            "winp": "微风",   //风力
            "weather_icon1": "http:\/\/api.k780.com:88\/upload\/weather\/n\/7.gif",
            "week": "星期二"   //星期
        },

从中提取出一些最简的信息来画结构:

技术分享

抽象出来的一天天气情况的实体类是这样的:

  1   2 
  3 import org.json.JSONException;
  4 import org.json.JSONObject;
  5 
  6 public class WeatherItem {
  7 
  8     private static final String JSON_DATE="date";
  9     private static final String JSON_WEEK="week";
 10     private static final String JSON_TEMP_LOW="temp_low";
 11     private static final String JSON_TEMP_HIGH="temp_high";
 12     private static final String JSON_HUMI_LOW="humi_low";
 13     private static final String JSON_HUMI_HIGH="humi_high";
 14     private static final String JSON_WEATHER="weather";
 15     private static final String JSON_WINP="winp";
 16     private static final String JSON_WIND="wind";
 17 
 18     private String date;//日期
 19     private String week;//星期
 20 
 21     private int temp_low;//最低温度
 22     private int temp_high;//最高温度
 23 
 24     private int humi_low;//最低湿度
 25     private int humi_high;//最高湿度
 26 
 27     private String weather;//天气
 28     private String winp;//风力
 29     private String wind;//风向
 30 
 31     public JSONObject toJson() throws JSONException{
 32         JSONObject json=new JSONObject();
 33         json.put(JSON_DATE,date);
 34         json.put(JSON_WEEK,week);
 35         json.put(JSON_TEMP_LOW,temp_low);
 36         json.put(JSON_TEMP_HIGH,temp_high);
 37         json.put(JSON_HUMI_LOW,humi_low);
 38         json.put(JSON_HUMI_HIGH,humi_high);
 39         json.put(JSON_WEATHER,weather);
 40         json.put(JSON_WINP,winp);
 41         json.put(JSON_WIND,wind);
 42         return json;
 43     }
 44 
 45     public String getDate() {
 46         return date;
 47     }
 48 
 49     public void setDate(String date) {
 50         this.date = date;
 51     }
 52 
 53     public String getWeek() {
 54         return week;
 55     }
 56 
 57     public void setWeek(String week) {
 58         this.week = week;
 59     }
 60 
 61     public int getTemp_low() {
 62         return temp_low;
 63     }
 64 
 65     public void setTemp_low(int temp_low) {
 66         this.temp_low = temp_low;
 67     }
 68 
 69     public int getTemp_high() {
 70         return temp_high;
 71     }
 72 
 73     public void setTemp_high(int temp_high) {
 74         this.temp_high = temp_high;
 75     }
 76 
 77     public int getHumi_low() {
 78         return humi_low;
 79     }
 80 
 81     public void setHumi_low(int humi_low) {
 82         this.humi_low = humi_low;
 83     }
 84 
 85     public int getHumi_high() {
 86         return humi_high;
 87     }
 88 
 89     public void setHumi_high(int humi_high) {
 90         this.humi_high = humi_high;
 91     }
 92 
 93     public String getWeather() {
 94         return weather;
 95     }
 96 
 97     public void setWeather(String weather) {
 98         this.weather = weather;
 99     }
100 
101     public String getWinp() {
102         return winp;
103     }
104 
105     public void setWinp(String winp) {
106         this.winp = winp;
107     }
108 
109     public String getWind() {
110         return wind;
111     }
112 
113     public void setWind(String wind) {
114         this.wind = wind;
115     }
116 }

以上是可以使用了转换成JSONObject对象的方法的,因为按照设想,每次fetch数据后都要保存起来,然后加载到view上,

要存起来的原因是为了减小每天从网上更新的次数,因为每次查询都是七天,那么就不需要每天多次频繁更新,

因此可以设置为每天更新一次(这里要用到Alarm,后续用到Widget的时候也是这个周期),

 

为了方便用户,若要加入手动更新的话,需要知道当前查询的是那些数据,

为了简便,我的设想就是将当前已经查询到的数据存储到一个json文件中,然后通过时间来命名,

把命名之后的文件名保存到一个配置文件中,每次更新或是想要fetch数据的时候都从配置文件中读取当前数据并同当前时间比较,相同就不更新,反之更新并覆盖原数据,更新配置文件。

 

下一阶段应当是解析json数据并转换成Item写出到文件中了。

自己实现简单的天气预报应用(2)

标签:

原文地址:http://www.cnblogs.com/lhyz/p/4312280.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!