天气项目·Bean

一、FutureWeatherBean

输出未来各天的天气情况吗,内含变量:temperture、 wind、 weather、week。

(一)初始化(Ctrl+1)

(二)get( )/set( )函数。(右键->source->Generate Getters and Setters)

二、WeatherBean

读取JSON数据。通过JSONObject来创建WeatherBean对象


JSON数据格式:键值对

相对应的,Bean里含有私有数据:cityName、cityId、week、dateY、weather、pm、pmLevel、wind、temperture、windLevel、ArrayListfutureWeatherList;

private ArrayList<FutureWeatherBean> futureWeatherList;//一个Bean的动态数组

(一)构造函数(Ctrl+1)

初:构造函数中,从JSON数据读出相应的数据。在mainActivity中有JsonObject对象:weatherJsonObject,用for循环从对象中读取出数据(weatherJsonObject.getString),并放入String对象。最后将读取出来的数据放入一个Bean对象。

此外,所有的try-catch中的异常都放大,如:IOException->Exception.

futureWeatherList = new ArrayList<FutureWeatherBea>();//初始化数组

try {    //会抛出异常,Ctrl+1,添加try-catch

cityName =weatherJsonObject.getString("city");

cityId = weatherJsonObject.getInt("cityid")...//通过JsonObject的key获取值,并赋给字符串

for (int i = 1; i < 7; i++) {

String futureWeather = weatherJsonObject.getString("weather"+ i);//json里的变量名是weather1

String futureWeek = getFutureWeekStringBy(week, i)...//依次获取futureTemperture, futureWind

FutureWeatherBean futureWeatherBean = new FutureWeatherBean(futureTemperture, futureWind, futureWeather, futureWeek);// 封装成一个FutureWeatherBean。注意有数组属性时一定要初始化数组才能使用

futureWeatherList.add(futureWeatherBean);}//添加到List里

} catch (Exception e) {

e.printStackTrace();}}

后:将部分程序进行封装,相应地改为:以String resultString作为返回值的构造方法。并新写private String dealWithResultString(String resultString) 方法。在try的开头添加:

String resultJsonString = dealWithResultString(resultString);//新添方法

JSONObject jsonObject = new JSONObject(resultJsonString);// 通过JSON字符串创建JSONObject的对象

JSONObject weatherJsonObject = jsonObject.getJSONObject("weatherinfo");//取出JSON数据里的"weatherinfo"并赋给weatherJsonObject

总而言之:构造方法是将JSON数据里有用的信息提取出来封装成一个FutureWeatherBean

实现方法:将JSON数据里的"weatherinfo"并赋给weatherJsonObject,再将weatherJsonObject里的数据依次取出赋值给String字符串再将各字符串封装成一个FutureWeatherBean

(二)get( )/set( )函数。(右键->source->Generate Getters and Setters)


(三)private String dealWithResultString(String resultString)


int startIndex = resultString.indexOf("(") + 1;// 找到左右括号在字符串中的位置。

int endIndex = resultString.indexOf(")");

return resultString.substring(startIndex, endIndex);// 根据左右括号的位置截取中间的JSON字符串。


(四)private String getFutureWeekStringBy(String week2, int itemIndex)

一个关于获取明天是"星期X"的小算法

ArrayList <String> weekStringList = new ArrayList();//new 一个动态数组,用于存放“星期X”

weekStringList.add("星期一")...//把“星期X”添加到StringList里

int currentDayIndex = weekStringList.indexOf(week);//今天的下标:值为“星期一”,对应下标为:0。(JSON里有键值对:"week": "星期一")

int futureDayIndex = currentDayIndex + itemIndex;//计算明天的下标

if (futureDayIndex > 6)    futureDayIndex -= 7;// 一共七天,循环

return weekStringList.get(futureDayIndex);//根据计算的下标返回String

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 34,922评论 18 399
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,770评论 19 139
  • importUIKit classViewController:UITabBarController{ enumD...
    明哥_Young阅读 4,211评论 1 10
  • 今年的毕业季,走了趟中国美院象山校区,作为一名跟设计好不搭边的普通人,新生的设计师们的毕业设计让我真切的感受到了设...
    柳絮XM阅读 595评论 7 14
  • 人之生也柔脆 其死也坚强 草木之生也柔弱 其死也枯槁 今天的内训让我看到了很多,很多平时我无法看到的事件,很多我无...
    4点零3阅读 340评论 0 0

友情链接更多精彩内容