电子与信息工程学院计算机科学与技术系 开放与创新实验设计报告
安卓实训设计报告
Android 天气预报
设计题目: Android天气预报
班 级:
姓 名:_
学 号:_
指导老师:
日 期: 20xx年6月7日
- 1 -
电子与信息工程学院计算机科学与技术系 开放与创新实验设计报告
内容要求
一、题目分析,功能要求。
1.1 实验目的
熟悉Android软件开发的基本架构
利用Eclipse和ADT插件设计贪天气预报
1.2 功能
本软件就是利用现有的网络快速获取网络上的天气信息并显示到手机终
端上,为用户提供实时的天气查询和近期天气查询服务,为工作、出行等带来便利。通过Web Service等技术让用户体验到前所未有的移动计算。
二、实验设计
本软件是一个App Widget应用程序,启动程序后可以进行城市、更新频率的设置,可以通过图片和文字显示当前和未来的天气状况,包括温度、湿度、风向和雨雪情况等。这些天气数据是通过后台服务获取的,这个后台服务可以按照一定的时间间隔,通过Google提供的服务获取天气预报信息,并将天气信息保存在数据库中。
该软件的基本功能需求有:
(1) 启动App Widget应用程序;
(2) 设置界面:对要显示天气预报的城市及更新频率进行设置;
(3) 显示界面:通过文字和图片显示当前的天气情况,包括日期、时间、城市、最高温度、最低温度、当前温度等。
(4) 详细界面:在显示出“显示界面”上所有信息的同时用列表的形式显示今后四天的天气情况。
三、实验程序
见附录一。
四、实验效果图图
- 2 -
电子与信息工程学院计算机科学与技术系 开放与创新实验设计报告
- 3 -
电子与信息工程学院计算机科学与技术系 开放与创新实验设计报告
五、总结
(1)在Android 平台上可以通过Web Service 技术方便的获取互联网上的资源信息,在智能手机强大的硬件功能和快速的移动互联网的支持下,可以更加方便的利用网络上的资源,为生活、工作带来极大的便利。
(2)Android语言的开发组件Activity、Intent Receiver、Service、Content Provider等提供了强大的数据处理功能。合理的设计数据库可以方便的实现各页面、进程之间的数据共享、数据的传递,也会使程序的运行更加稳定。
(3)Android系统中的视图组件View和View Group等的灵活使用,可以设计出界面美观,用户操作方便灵活的页面,并可以实现页面各种不同的显示布局以及动画效果等。
(4)使用SAX的方式解析android应用程序中的XML数据文件,可以提供很好的性能需求。之所以选择SAX的方式来解析XML文件,是因为它是一种非常优秀的轻量级解决方案。使用android平台的硬件环境主要是手机和其它一些嵌入式设备。这些设备都需要比较快的解析速度和尽可能少的内存占用,同时满足这两方面要求的技术中SAX是一个非常好的选择。
- 4 -
电子与信息工程学院计算机科学与技术系 开放与创新实验设计报告
附录一
布局
1. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout xmlns:android="/apk/res/android"
3. android:orientation="vertical"
4. android:layout_width="fill_parent"
5. android:layout_height="fill_parent"
6. >
7. <TextView
8. android:id="@+id/tvPlace"
9. android:layout_width="fill_parent"
10. android:layout_height="wrap_content"
11. />
12. <EditText
13. android:id="@+id/place"
14. android:layout_width="fill_parent"
15. android:layout_height="wrap_content"
16. android:hint="输入城市名称(汉字或者拼音)"
17. />
18. <Button
19. android:id="@+id/query"
20. android:layout_width="fill_parent"
21. android:layout_height="wrap_content"
22. android:text="查询"
23. />
24. </LinearLayout>
将网络图片转换为android的bitmap对象
25. package com.sbs.weather;
26. import java.io.IOException;
27. import java.io.InputStream;
28. import java.net.HttpURLConnection;
29. import java.net.MalformedURLException;
30. import java.net.URL;
31. import android.graphics.Bitmap;
32. import android.graphics.BitmapFactory;
33. public class Utils {
34. public static String week(String enWeek) {
35. if (enWeek.equals("Mon") || enWeek.equals("Monday"))
36. return "星期一";
37. else if (enWeek.equals("Tue") || enWeek.equals("Tuesday"))
38. return "星期二";
39. else if (enWeek.equals("Wed") || enWeek.equals("Wednesday"))
- 5 -
电子与信息工程学院计算机科学与技术系 开放与创新实验设计报告
40. return "星期三";
41. else if (enWeek.equals("Thu") || enWeek.equals("Thursday"))
42. return "星期四";
43. else if (enWeek.equals("Fri") || enWeek.equals("Friday"))
44. return "星期五";
45. else if (enWeek.equals("Sat") || enWeek.equals("Saturday"))
46. return "星期六";
47. else if (enWeek.equals("Sun") || enWeek.equals("Sunday"))
48. return "星期日";
49. return "";
50. }
51. public static String weather(String enWeather) {
52. if (enWeather.equals("Clear"))
53. return "晴";
54. else if (enWeather.equals("Partly Sunny")
55. || enWeather.equals("partly_cloudy"))
56. return "多云";
57. else if (enWeather.equals("Chance of Rain"))
58. return "晴转雨";
59. else if (enWeather.equals("storm"))
60. return "暴雨";
61. else if (enWeather.equals("thunderstorm"))
62. return "雷阵雨";
63. else if (enWeather.equals("fog"))
64. return "大雾";
65. else if (enWeather.equals("haze"))
66. return "有雾";
67. else if (enWeather.equals("rain"))
68. return "雨";
69. else if (enWeather.equals("heavyrain"))
70. return "大雨";
71. else if (enWeather.equals("lightrain"))
72. return "小雨";
73. else if (enWeather.equals("heavyrain"))
74. return "大雨";
75. else if (enWeather.equals("snow"))
76. return "有雪";
77. // / 还需要补充。。。。
78. return "";
79. }
80. public static Bitmap returnBitMap(String imgUrl) {
81. URL myImgUrl = null;
82. Bitmap bitmap = null;
83. try {
84. myImgUrl = new URL(imgUrl);
85. } catch (MalformedURLException e) {
- 6 -
电子与信息工程学院计算机科学与技术系 开放与创新实验设计报告
86. e.printStackTrace();
87. }
88. try {
89. HttpURLConnection conn = (HttpURLConnection) myImgUrl
90. .openConnection();
91. conn.setDoInput(true);
92. conn.connect();
93. InputStream is = conn.getInputStream();
94. bitmap = BitmapFactory.decodeStream(is);
95. is.close();
96. } catch (IOException e) {
97. e.printStackTrace();
98. }
99. return bitmap;
100. }
101. }
下面的程序通过Google提供的api来获取天气预报信息
1. package com.sbs.weather;
2. import java.io.InputStream;
3. import javax.xml.parsers.DocumentBuilder;
4. import javax.xml.parsers.DocumentBuilderFactory;
5. import org.apache.http.HttpEntity;
6. import org.apache.http.HttpResponse;
7. import org.apache.http.client.methods.HttpGet;
8. import org.apache.http.client.methods.HttpUriRequest;
9. import org.apache.http.impl.client.DefaultHttpClient;
10. import org.apache.http.util.EntityUtils;
11. import org.w3c.dom.Document;
12. import org.w3c.dom.NodeList;
13. import org.xml.sax.InputSource;
14. import android.app.Activity;
15. import android.graphics.Bitmap;
16. import android.os.Bundle;
17. import android.os.Handler;
18. import android.os.Message;
19. import android.util.Log;
20. import android.view.View;
21. import android.widget.Button;
22. import android.widget.EditText;
23. import android.widget.ImageView;
24. import android.widget.TextView;
25. public class Weather extends Activity {
26. public EditText ETplace;
27. public TextView TvPlace;
- 7 -
电子与信息工程学院计算机科学与技术系 开放与创新实验设计报告
28. public Button query;
29. public TextView placeName;
30. public ImageView imView;
31. /** Called when the activity is first created. */
32. @Override
33. public void onCreate(Bundle savedInstanceState) {
34. super.onCreate(savedInstanceState);
35. setContentView(R.layout.main);
36.
37. ETplace = (EditText)findViewById(R.id.place);
38. query = (Button)findViewById(R.id.query);
39. imView = (ImageView)findViewById(R.id.myImageView);
40. placeName = (TextView)findViewById(R.id.placeName);
41. query.setOnClickListener(new Button.OnClickListener() {
42. public void onClick(View v) {
43. try{
44. TvPlace = (TextView)findViewById(R.id.tvPlace);
45. String place = CntoEn.getFullSpell(ETplace.getText().toString());
46. placeName.setText(place);
47. String weather = "";
48. String url = "/ig/api?&weather="+place;
49. DefaultHttpClient client = new DefaultHttpClient();
50. HttpUriRequest req = new HttpGet(url);
51. HttpResponse resp = client.execute(req);
52. //String strResult = EntityUtils.toString(resp.getEntity());
53. //Log.i("weather->", strResult);
54. //一华氏度等于9/5摄氏度数值+32
55. HttpEntity ent = resp.getEntity();
56. InputStream stream = ent.getContent();
57. DocumentBuilder b = DocumentBuilderFactory.newInstance()
58. .newDocumentBuilder();
59. Document d = b.parse(new InputSource(stream));
60. NodeList n = d.getElementsByTagName("forecast_conditions");
61. // 获得图片url 当天的。
62. String imgUrl = "";
63. imgUrl += n.item(0).getChildNodes().item(3).getAttributes().item(0).getNodeValue();
64. imView.setImageBitmap(Utils.returnBitMap(imgUrl));
65. // 今后4天预报
66. for (int i = 0; i < n.getLength(); i++) {
67. weather += Utils.week(n.item(i).getChildNodes().item(0)
68. .getAttributes().item(0).getNodeValue());
69. weather += ", ";
70. weather += (Integer
71. .parseInt(n.item(i).getChildNodes().item(1)
72. .getAttributes().item(0).getNodeValue()) - 32) * 5 / 9;
73. weather += " ~ ";
- 8 -
电子与信息工程学院计算机科学与技术系 开放与创新实验设计报告
74. weather += (Integer
75. .parseInt(n.item(i).getChildNodes().item(2)
76. .getAttributes().item(0).getNodeValue()) - 32) * 5 / 9;
77. weather += ", ";
78. weather += Utils.weather(n.item(i).getChildNodes().item(4)
79. .getAttributes().item(0).getNodeValue());
80. weather += "\n";
81. }
82. Log.i("parseed weather->", weather);
83. TvPlace.setText(weather);
84.
85. } catch (Exception e) {
86. e.printStackTrace();
87. }
88.
89. }});
90. }
91. }
添加权限
1. <?xml version="1.0" encoding="utf-8"?>
2. <manifest xmlns:android="/apk/res/android"
3. package="com.sbs.weather"
4. android:versionCode="1"
5. android:versionName="1.0">
6. <uses-permission android:name="android.permission.INTERNET"></uses-permission>
7. <application android:icon="@drawable/icon" android:label="@string/app_name">
8. <activity android:name=".Weather"
9. android:label="@string/app_name">
10. <intent-filter>
11. <action android:name="android.intent.action.MAIN" />
12. <category android:name="android.intent.category.LAUNCHER" />
13. </intent-filter>
14. </activity>
15. </application>
16. </manifest>
- 9 -