Develop And Improve。
用了两天时间,终于把困扰了我许久的多层树结构JSON数据的解析搞定了,用了一种很简易的方法。
对于每一层数据,都要对其关键字建立一个实体类,里面定义这一层数据的数据元素,并设置get方法,对于整个JSON数据串也要建立一个实体类存放所有关键字,Result实体类代码如下
1 | public class Result { |
如下图所示对所需元素建立好实体类
1 | public class Now { |
以这个类为例,将实体元素定义好便可直接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
51public 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 | public static void saveWeatherInfoBasic(Context context,String cityName,String tempNow,String feelTemp,String currentCondition){ |
这样对应元素储存好后便可在ShowWeatherActivity中直接绑定窗体属性,只取部分代码
1 | private void showWeather(){ |
至于获取JSON数据方法很简单,使用百度ApiStoreSDK直接调用即可
1 | private void apiTest(String countyName) { |
完善逻辑,重写Back事件以及实现下拉刷新功能