YOU'VE ENTERED A SIMPLE ANDROIDBLOG, WELCOME.

Develop And Improve。

极简天气开发日记Day04

Things have done

用了两天时间,终于把困扰了我许久的多层树结构JSON数据的解析搞定了,用了一种很简易的方法。

对于每一层数据,都要对其关键字建立一个实体类,里面定义这一层数据的数据元素,并设置get方法,对于整个JSON数据串也要建立一个实体类存放所有关键字,Result实体类代码如下

1
2
3
4
5
6
7
8
9
10
public class Result {
@SerializedName("HeWeather data service 3.0")
private List<Weather> weatherInfoList;

public List<Weather> getWeatherInfoList() {
return weatherInfoList;
}


}

如下图所示对所需元素建立好实体类

http://pic.yupoo.com/333ddd/FPg5CsrJ/15m52p.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public class Now {
/**
* "cond": {} //天气状况
* "fl": "30", //体感温度
* "hum": "20%", //相对湿度(%)
* "pcpn": "0.0", //降水量(mm)
* "pres": "1001", //气压
* "tmp": "32", //温度
* "vis": "10", //能见度(km)
*/

private Cond cond;
private String fl;
private String hum;
private String tmp;


public Cond getCond() {
return cond;
}

public void setCond(Cond cond) {
this.cond = cond;
}

public String getFl() {
return fl;
}

public void setFl(String fl) {
this.fl = fl;
}

public String getHum() {
return hum;
}

public void setHum(String hum) {
this.hum = hum;
}

public String getTmp() {
return tmp;
}

public void setTmp(String tmp) {
this.tmp = tmp;
}
}

以这个类为例,将实体元素定义好便可直接get到JSON数据中对应的属性值

下面代码中,我在Utility类中建立了handleWeatherResponse方法处理JSON数据,由于需要解析的元素比较多,我按照日期进行了划分,并在调用数据储存方法时候分开处理减少出错率

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public static void handleWeatherResponse(Context context,String response){
Gson gson=new Gson();
Result result = gson.fromJson(response, Result.class);
List<Weather> weatherInfoList = result.getWeatherInfoList();
Weather weatherInfo = weatherInfoList.get(0);
Aqi aqi=weatherInfo.getAqi();
Now now=weatherInfo.getNow();
Basic basic=weatherInfo.getBasic();
String cityName=basic.getCity();
String tempNow=now.getTmp();
String feelTemp=now.getFl();
String currentCondition=now.getCond().getTxt();

//initialize today
DailyForecast dailyforecast_today =weatherInfo.getDaily_forecast().get(0);
Astro astro_today= dailyforecast_today.getAstro();
String sunRiseToday=astro_today.getSr();
String sunDownToday=astro_today.getSs();
String tempBelowToday= dailyforecast_today.getTmp().getMin();
String tempHighToday= dailyforecast_today.getTmp().getMax();
String wet= dailyforecast_today.getHum();
String windDir=dailyforecast_today.getWind().getDir();
String windSc=dailyforecast_today.getWind().getSc();

//initialize day1
DailyForecast dailyforecast_day1 =weatherInfo.getDaily_forecast().get(1);
String timeDay1= dailyforecast_day1.getDate();
String conditionDay1= dailyforecast_day1.getCond().getTxt_d();
String tempBelowDay1= dailyforecast_day1.getTmp().getMin();
String tempHighDay1= dailyforecast_day1.getTmp().getMax();

//initialize day2
DailyForecast dailyforecast_day2 =weatherInfo.getDaily_forecast().get(2);
String timeDay2= dailyforecast_day2.getDate();
String conditionDay2= dailyforecast_day2.getCond().getTxt_d();
String tempBelowDay2= dailyforecast_day2.getTmp().getMin();
String tempHighDay2= dailyforecast_day2.getTmp().getMax();

//initialize day3
DailyForecast dailyforecast_day3 =weatherInfo.getDaily_forecast().get(3);
String timeDay3= dailyforecast_day3.getDate();
String conditionDay3= dailyforecast_day3.getCond().getTxt_d();
String tempBelowDay3= dailyforecast_day3.getTmp().getMin();
String tempHighDay3= dailyforecast_day3.getTmp().getMax();

saveWeatherInfoBasic(context, cityName, tempNow, feelTemp, currentCondition);
saveWeatherInfoToday(context,sunRiseToday,sunDownToday,tempBelowToday,tempHighToday,wet,windDir,windSc);
saveWeatherInfoDay1(context, timeDay1, conditionDay1, tempBelowDay1, tempHighDay1);
saveWeatherInfoDay2(context, timeDay2, conditionDay2, tempBelowDay2, tempHighDay2);
saveWeatherInfoDay3(context, timeDay3, conditionDay3, tempBelowDay3, tempHighDay3);
}

对应的save方法很简单,使用SharePreferences来储存数据

1
2
3
4
5
6
7
8
9
public static void saveWeatherInfoBasic(Context context,String cityName,String tempNow,String feelTemp,String currentCondition){
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
editor.putString("city_name", cityName);
editor.putString("temp_now", tempNow);
editor.putString("feel_temp", feelTemp);
editor.putString("current_condition", currentCondition);
editor.commit();

}

这样对应元素储存好后便可在ShowWeatherActivity中直接绑定窗体属性,只取部分代码

1
2
3
4
5
6
7
private void showWeather(){
SharedPreferences sharedPreferences= PreferenceManager.getDefaultSharedPreferences(this);
city_name.setText(sharedPreferences.getString("city_name","Null"));
temp_now.setText(sharedPreferences.getString("temp_now","Null") + "℃");
feel_temp.setText(sharedPreferences.getString("feel_temp","Null") + "℃");
current_condition.setText(sharedPreferences.getString("current_condition","Null"));
}

至于获取JSON数据方法很简单,使用百度ApiStoreSDK直接调用即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
private void apiTest(String countyName) {

ApiStoreSDK.init(getApplicationContext(), "*****************");
Parameters para = new Parameters();
para.put("city", countyName);
ApiStoreSDK.execute("http://apis.baidu.com/heweather/weather/free",
ApiStoreSDK.GET,
para,
new ApiCallBack() {

@Override
public void onSuccess(int status, String responseString) {

Log.d("test", responseString);
Utility.handleWeatherResponse(ShowWeatherActivity.this, responseString);
showWeather();
Log.i("test", "onSuccess" + responseString);
}

@Override
public void onComplete() {
Log.i("test", "onComplete");
}

@Override
public void onError(int status, String responseString, Exception e) {
Log.i("test", "onError, status: " + status);
Log.i("test", "errMsg: " + (e == null ? "" : e.getMessage()));
}
});

}

Things to do

完善逻辑,重写Back事件以及实现下拉刷新功能