安卓天气预报APP学校:武汉理工大学学院:计算机科学与技术学院姓名:张宁摘要:科技的进步会给人们的生活带来方便和快捷,随着科技的发展,移动智能终端逐渐走进人们的视线,相关应用越来越广泛,并在人们的日常生活中扮演着越来越重要的角色。
因此,关键应用程序的开发成为影响移动智能终端普及的重要因素,设计并开发实用、方便的应用程序具有重要的意义和良好的市场前景。
Android作为当前最流行的操作平台,自然也存在着大量的应用服务需求。
本课题研究的是基于Android平台下的天气软件,目的是帮助用户随时随地查询天气信息及天气变化情况,让生活更加舒适方便。
本文详细讲述了Android平台下天气软件的界面设计及异常处理部分,主要包括界面的实现、控件的使用、界面的布局和异常的处理等内容,结合中国气象网的天气数据,将准确的天气情况以友好的方式呈现给用户,使用户可以更加赏心悦目地体验本软件的服务。
目录第一章绪论1.1研究背景1.2研究意义1.3研究内容第二章系统分析2.1研究目标2.2需求分析2.3性能分析第三章程序设计与实现3.1用户界面的实现3.2获取和解析数据3.3在UI上呈现天气信息第4章系统测试第5章结论5.1总结与改进5.1参考文献第一章绪论1.1研究背景随着科技的发展,Android在中国的前景十分广阔,它的社区也十分红火,而这些社区为Android在中国市场的普及做了很好的推广作用。
国内厂商和许多大运营商也纷纷加入了Android的阵营,同时不仅仅是局限于手机上,国内的厂家也不断的推出了Android系统的产品。
Android的主要竞争对手有苹果的IOS以及RIM的Blackberry OS。
在2011年的第一季度中,Android在全球的市场份额首次超过了塞班系统,跃居全球第一。
于次年的2月份,Android占据全球智能手机操作系统市场的52.5%,而在中国的市场占有率为68.4%,在美国的手机市场上占有率达到了47.3%。
到同年的二月下旬,Google公司正式宣布,Android设备每天激活量高达85万台。
Google的官方数据表明,Android在电子市场上的应用程序数量已经突破45万。
相信在不久的将来,会有越来越多的用户选择Android系统的手机。
Android系统是以Linux为核心的手机操作平台,并且它是作为一款开放式的、可扩展的操作系统。
随着Android的快速发展,如今它已经允许开发者使用多种编程语言来编写Android的应用程序,而不再是像以前一样,只能使用Java语言来开发Android应用程序。
因此,它受到了许许多多的开发者的欢迎,成为了真正意义上的开放式操作系统。
1.2研究意义天气预报是一种具有实用的信息服务,伴随着智能手机的不断普及和应用,各种智能手机平台下都有不同的天气预报软件。
Android作为现在主流的智能手机之一,自然也有这方面的软件。
天气状况常常关系到人们的日常生活,比如它能告知温暖,提供方便出行等等。
此外,它对未来天气的预测还可以帮助农民对农作物的浇灌、施肥、收割等一系列的工作指导,以便让他们提前做出防御措施,?把气象灾害带来的损失降到最低程度,并且能使得广大公众在第一时间方便、快捷地获取最新的天气预报信息。
同时它也可以强化天气公共服务的职能,改善公共服务手段、完善公共服务体系、提高公共服务质量,更好地发挥了气象事业对社会发展的现实性作用。
? 这款手机天气预报系统给人们生活带来方便的全方位信息,是人们在日常生活中必不可少的生活用品。
此天气预报软件的界面明了,操作简单,适合各类人士,对提高人们生活水平具有?一定的促进作用。
1.3研究内容Android平台的天气预报系统是基于Android手机平台,利用Java语言,从Google上获取天气预报的信?息,开发出对使用Android平台的手机天气预报系统的一款应用软件。
? 为了让智能手机用户能够随时随地查询互联网所提供的服务,让手机能通过互联网访?问Web?网站并处理各项不同的业务,其中有一种非常高效的方法就是将应用系统的功能拓展到手机的客户端。
Android?的一个最大特点就是它的开放性架构体系,它不仅仅具有非常好的调试环境和开发环境,而且它还具有各种各样的可扩展功能,其中还包括丰富的多媒体功能和图形组件的强大浏览器,而且其技术已经逐渐趋于成熟。
通过对界面的布局和控件使用等相关知识的学习和应用,使我对Android平台下的开发工作有了一个初步的了解,并且经过这次的开发,它使我能够基本掌握这些重要内容。
软件开发以后,它能够帮助用户随时搜索由中国气象局提供的全国范围内的城市的天气信息,并且把数据显示在一个界面美观、布局合理窗口内,使用户能够舒适的享受此软件带来的简便的服务。
第2章系统分析2.1研究目标1.要了解和认识Android应用程序的开发和设计过程;?2.运?用Web Service来实现Android?平台的手机天气预报系统的设计。
? 此软件是基于Eclipse的开发环境下,利用Google天气的气象数据建立起天气预报信息数据库,?从而开发出了针对于Android平台的手机天气预报软件。
2.2需求分析在当代社会中,手机已经发展成为广大人们群众的日常生活中必不可少的通讯工具,?而且,手机的功能也逐渐趋于完善。
手机用户可以选择开通GPRS网络服务,这种服务的方便和快捷,使人们对于手机上网的需求得到了保证,从而带动了手机软件的发展,并且让我国的移动通讯功能也得到了大大的提高。
越来越多的手机软件被用在手机上,弥补和完善了原始系统的不足。
随着科技的发展,现在手机的功能越来越多,也越来越强大,再也不像以前那么死板、简单。
此软件是在现有手机的基础上,通过网络查询实现天气状况的报告和未来天气的预报等功能,让人们的出行和生活得到方便和保证。
此软件是一个利用中国气象网来获取天气的应用程序,启动程序后可以进行城市之间的选择的设置,并且可以通过图片、文字来显示当前和未来几天的天气情况,其中包括温度、湿度、风向和雨雪等情况。
这些天气的数据是通过后台服务获取的,并从网上上获取天气的相关信息,将天气的信息保存在数据库中,方便用户的更新和查看。
2.3性能分析1.点击Weather.apk安装;2进入主界面后,看到的是默认的城市和它的天气情况;3点击右上角的“城”按钮,进行其他城市的选择,点击保存即可获得该城市的天气预报信息;? 此软件操作十分简单,设计的比较人性化,应用也很方便,安全性能较高、程序的响应速度也很快、信息处理速度快。
第3章程序设计与实现3.1用户界面的实现Android提供的UI开发模块是基于XML文件的,这些XML 文件放在res/layout下面。
此目录可包含所有应用程序的非代码部分,比如图片、xml文件等。
如果要使用这些资源,在代码目录下打开R.java文件即可。
在xml中编写界面代码,实现的时候方便和灵活。
在Android程序设计中要用到一些基本的UI元素,通过运用Views、ViewGroups和layout可为Activity创建直观的UI。
通常用Android SDK中的一些控件,并用ViewGroups去组合Views,实现数据的显示。
在一个Android的应用中,用户界面由View和ViewGroup对象构建。
View对象是Android平台中用户界面的基本单元。
View类是widgets(工具)类的父类,它们提供了如文本输入框和按钮之类的UI。
VieGroup类是Layout(布局)类的父类,它们提供了如表格布局和布局的架构等。
View是一个数据体,它存储了屏幕上一个区域的布局参数和内容,并负责它所管辖的区域中的测量、卷动和按键手势的处理。
为了创建Android平台的用户界面,首先要在PackageExplorer中展开layout后新建即可。
XML文件用来对窗口进行布局,主要有系统运行的配置文件有main.xml和result.xml。
Layout是一类ViewGroup控件,它本身没有内容,存在的唯一原因就是其中的结构能放好它的子控件。
比如,Linearlayout可以将子控件按水平或垂直方向排列;Tablelayout可以将子控件按表格形式依次放好。
所以,有了Layout,控件和控件之间不再是分裂开的。
其主要代码是:<ScrollViewxmlns:android="/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:scrollbars="vertical"android:fadingEdge="vertical"android:background="@drawable/back1"><RelativeLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:scrollbars="vertical"android:fadingEdge="vertical"><TableLayout android:id="@+id/table"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/weatherbg"><TableRowandroid:layout_width="fill_parent"android:layout_height="wrap_content"><LinearLayout android:layout_width="wrap_content"android:layout_height="fill_parent"android:gravity="center"android:orientation="vertical"><TextView android:id="@+id/cityname"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textStyle="bold"android:textColor="#2F4F4F"android:paddingLeft="25dp"android:layout_gravity="center"android:textSize="28dp"android:text="未知"/><TextView android:id="@+id/tempe"android:layout_width="fill_parent"android:layout_height="wrap_content"android:paddingLeft="40dp"android:textColor="#FF8C00"android:textSize="10dp"android:text=""/></LinearLayout><ImageViewandroid:id="@+id/icon"android:layout_width="100dp"android:layout_height="50dp"android:layout_gravity="center"/></TableRow><TableRowandroid:layout_width="fill_parent"android:layout_height="wrap_content"><LinearLayout android:layout_width="wrap_content"android:layout_height="wrap_content"android:paddingLeft="30dp"android:gravity="center"android:orientation="vertical"><TextView android:id="@+id/tdate"android:layout_width="fill_parent"android:layout_height="wrap_content"android:textSize="14dp"android:textColor="#E0FFFF"android:text="时间:2012年12月12日"/><TextView android:id="@+id/tweek"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_gravity="right"android:textColor="#E0FFFF"android:textSize="12dp"android:text="星期:星期三"/></LinearLayout><TextView android:id="@+id/weather"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:textColor="#FAFAD2"android:gravity="bottom"android:textSize="18dp"android:text="晴"/></TableRow></TableLayout><LinearLayout android:layout_toRightOf="@id/table"android:paddingTop="40dp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><Button android:id="@+id/setcity"android:layout_width="40dp"android:layout_height="40dp"android:background="@drawable/button_place"android:visibility="gone"/><Button android:id="@+id/refresh"android:layout_width="40dp"android:layout_height="40dp"android:background="@drawable/button_fresh"/></LinearLayout><LinearLayout android:id="@+id/clothes"android:layout_width="wrap_content"android:layout_below="@id/table"android:layout_height="wrap_content"android:gravity="center"android:orientation="horizontal"><ImageViewandroid:id="@+id/clothesicon"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/clothes"android:layout_gravity="top"/><TextView android:id="@+id/clothesvalue"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="#FF8C00"android:textSize="16dp"android:text="穿衣指数"/></LinearLayout><TextView android:id="@+id/title"android:layout_below="@id/clothes"android:layout_width="fill_parent"android:layout_height="wrap_content"android:textSize="18dp"android:textColor="#FF4500"android:background="#77A9A9A9"android:text="生活指数:"/><ListView android:id="@+id/lifevalue"android:headerDividersEnabled="false"android:fitsSystemWindows="false"android:layout_below="@id/title"android:layout_width="wrap_content"android:layout_height="180dp"android:scrollbars="vertical"></ListView><Button android:id="@+id/forecast"android:layout_below="@id/lifevalue"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginBottom="10dp"android:textSize="18dp"android:textColor="#FF4500"android:background="@drawable/button_long"android:text="未来六天天气预报"/> <TextViewandroid:id="@+id/test"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@+id/forecast2"android:text="Button"android:textColor="#ff0033"/><LinearLayoutandroid:id="@+id/forecast2"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_below="@+id/forecast"android:orientation="vertical"></LinearLayout></RelativeLayout></ScrollView>3.2 获得和解析数据首先是httpget 主要代码如下:private String connServerForResult(String strUrl) {// 获取HttpGet对象HttpGet httpRequest = new HttpGet(strUrl);// httpRequest.String strResult = "";try {// HttpClient对象HttpClient httpClient = new DefaultHttpClient();// 获得HttpResponse对象HttpResponse httpResponse =httpClient.execute(httpRequest);if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {// 取得返回的数据strResult =EntityUtils.toString(httpResponse.getEntity());}} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}if (strResult.charAt(0) != '{')strResult = '{' + strResult;if (strResult.charAt(strResult.length() - 1) != '}') strResult = strResult + '}';return strResult;}然后对获得的json数据进行解析,主要代码:// 普通Json数据解析private void parseJson(String strResult) {try {JSONObject jsonObj = new JSONObject(strResult).getJSONObject("weatherinfo");// JSONArray jsonArray =jsonObj.getJSONArray("calendarlist");// 清空原数据lifeV.clear();tempes.clear();weathers.clear();winds.clear();fls.clear();// 打开SharedPreferences更新数据Editor write = dataofday.edit();today = jsonObj.getString("date_y"); // 当前日期dayofweek = jsonObj.getString("week"); // 当前星期city = jsonObj.getString("city"); // 城市名称ftime = jsonObj.getInt("fchh");// 更新时间(整点)【更新时间确定temp1属于哪天】write.putString("cityid", cityid);// 城市IDwrite.putInt("ftime", ftime);// 更新时间write.putString("city", city);write.putString("today", today);write.putString("dayofweek", dayofweek);for (int i = 1; i <= 6; i++) {// 六天天气的图片Idimages[i - 1] = jsonObj.getInt("img" + (i * 2 - 1));write.putInt("img" + (i * 2 - 1),jsonObj.getInt("img" + (i * 2 - 1)));// 六天的气温(摄氏度)tempes.add(jsonObj.getString("temp" + i));write.putString("temp" + i, jsonObj.getString("temp" + i));// 六天的天气描述weathers.add(jsonObj.getString("weather" + i));write.putString("weather" + i,jsonObj.getString("weather" + i));// 六天的风力大小及风向winds.add(jsonObj.getString("wind" + i));write.putString("wind" + i, jsonObj.getString("wind" + i));// 六天的风力fls.add(jsonObj.getString("fl" + i));write.putString("fl" + i, jsonObj.getString("fl" + i));}// 生活指数lifeV.add(jsonObj.getString("index"));lifeV.add(jsonObj.getString("index_d"));lifeV.add(jsonObj.getString("index48"));lifeV.add(jsonObj.getString("index48_d"));lifeV.add(jsonObj.getString("index_uv"));lifeV.add(jsonObj.getString("index48_uv"));lifeV.add(jsonObj.getString("index_xc"));lifeV.add(jsonObj.getString("index_tr"));lifeV.add(jsonObj.getString("index_co"));lifeV.add(jsonObj.getString("index_cl"));lifeV.add(jsonObj.getString("index_ls"));lifeV.add(jsonObj.getString("index_ag"));// 写入保存数据write.putString("index", lifeV.get(0));write.putString("index_d", lifeV.get(1));write.putString("index48", lifeV.get(2));write.putString("index48_d", lifeV.get(3));write.putString("index_uv", lifeV.get(4));write.putString("index48_uv", lifeV.get(5));write.putString("index_xc", lifeV.get(6));write.putString("index_tr", lifeV.get(7));write.putString("index_co", lifeV.get(8));write.putString("index_cl", lifeV.get(9));write.putString("index_ls", lifeV.get(10));write.putString("index_ag", lifeV.get(11));// 提交数据mit();} catch (JSONException e) {return;}3.3 在UI上呈现天气信息主要代码如下:protected void onCreate(Bundle savedInstanceState) { // 天气预报页面super.onCreate(savedInstanceState);setContentView(yout.activity_weather);cityname = (TextView) findViewById(R.id.cityname);test = (TextView) findViewById(R.id.test);date = (TextView) findViewById(R.id.tdate);week = (TextView) findViewById(R.id.tweek);temp = (TextView) findViewById(R.id.tempe);// 温度weather = (TextView) findViewById(R.id.weather);clothesv = (TextView) findViewById(R.id.clothesvalue);icon0 = (ImageView) findViewById(R.id.icon);forecast = (Button) findViewById(R.id.forecast);forecast2 = (LinearLayout) findViewById(R.id.forecast2); refresh = (Button) findViewById(R.id.refresh);Dialog = new ProgressDialog(WeatherActivity.this);setListener();// 设置监听器lifeV = new ArrayList<String>();tempes = new ArrayList<String>();weathers = new ArrayList<String>();winds = new ArrayList<String>();fls = new ArrayList<String>();lifeValue = (ListView) findViewById(R.id.lifevalue);dataofday = getSharedPreferences(PREFS_NAME, 0);// 获取数据ftime = dataofday.getInt("ftime", -1);today = dataofday.getString("today", "2015年2月22日");city = dataofday.getString("city", "武汉");dayofweek = dataofday.getString("dayofweek", "星期日");for (int i = 1; i <= 6; i++) {// 图片Idimages[i - 1] = dataofday.getInt("img" + (i * 2 - 1), 0);// 六天温度tempes.add(dataofday.getString("temp" + i, "0"));// 六天天气描述weathers.add(dataofday.getString("weather" + i, "未知"));// 六天风的描述winds.add(dataofday.getString("wind" + i, "未知"));// 六天风力描述fls.add(dataofday.getString("fl" + i, "未知"));}// 生活指数lifeV.add(dataofday.getString("index", "未知"));lifeV.add(dataofday.getString("index_d", "未知"));lifeV.add(dataofday.getString("index48", "未知"));lifeV.add(dataofday.getString("index48_d", "未知"));lifeV.add(dataofday.getString("index_uv", "未知"));lifeV.add(dataofday.getString("index48_uv", "未知"));lifeV.add("洗车指数:" + dataofday.getString("index_xc", "未知"));lifeV.add("旅游指数:" + dataofday.getString("index_tr", "未知"));lifeV.add("舒适指数:" + dataofday.getString("index_co", "未知"));lifeV.add("晨跑指数:" + dataofday.getString("index_cl", "未知"));lifeV.add("晾晒指数:" + dataofday.getString("index_ls", "未知"));lifeV.add("息斯敏过敏气象指数:"+ dataofday.getString("index_ag", "未知"))第四章系统测试在完成大部分代码以后我进行了测试,出现了好多问题,而且原家里网络很差,无法获得及时的帮助,自己以为是个别的人为错误,后来发现是中国天气网的api换了,而且官网打不开,所以致使软件一直无法完成。