展示一个简单的Android天气查询,获取的是Google的XML。

1.这里是一个解析XML的主题类,继承DefaultHandler,使用里面的startDocument(),endDocument().startElement().endElement().对XML进行解析。

GoogleWeatherHandler
  1 public class GoogleWeatherHandler extends DefaultHandler {
2
3 private boolean in_forecast_information = false;
4 private boolean in_current_conditions = false;
5 private boolean in_forecast_conditions = false;
6 //温度
7 private Integer current_temp;
8 //天气情况
9 private String current_condition;
10 //湿度
11 private String current_hum;
12 //图标
13 private String iconURL;
14 // false为华氏度,true为摄氏度
15 private boolean usingSITemperature = false;
16
17 //下面为get.set方法
18 public Integer getCurrentTemp(){
19 return this.current_temp;
20 }
21 public String getCurrentCondition(){
22 return this.current_condition;
23 }
24 public String getCurrentHum(){
25 return this.current_hum;
26 }
27 public String getIconURL(){
28 return this.iconURL;
29 }
30 public void setCurrentTemp(Integer temp) {
31 this.current_temp = temp;
32 }
33 public void setCurrentCondition(String condition) {
34 this.current_condition = condition;
35 }
36 public void setCurrentHum(String hum) {
37 this.current_hum = hum;
38 }
39 public void setIconURL(String iconURL) {
40 this.iconURL = iconURL;
41 }
42
43 //此方法是开始解析XML的启动项
44 public void startDocument() throws SAXException {
45
46 }
47
48 //此方法是最后解析XML的返回项
49 public void endDocument() throws SAXException {
50
51 }
52
53 //解析XML单元格的方法
54 //1.第一个参数为解析的URI
55 //2.解析标题前
56 //3.解析标题后
57 //4.存储解析标题内容
58 public void startElement(String namespaceURI, String localName,
59 String qName, Attributes atts) throws SAXException {
60 //这里的判断是为了解析那个单元格
61 if (localName.equals("forecast_information")) {
62 this.setIn_forecast_information(true);
63 } else if (localName.equals("current_conditions")) {
64 this.in_current_conditions = true;
65 } else if (localName.equals("forecast_conditions")) {
66 this.in_forecast_conditions = true;
67 }
68 else {
69 //这里是建立一个标题内容赋值对象
70 String dataAttribute = atts.getValue("data");
71 //解析标题
72 if (localName.equals("city")) {
73 } else if (localName.equals("postal_code")) {
74 } else if (localName.equals("latitude_e6")) {
75 /* One could use this to convert city-name to Lat/Long. */
76 } else if (localName.equals("longitude_e6")) {
77 /* One could use this to convert city-name to Lat/Long. */
78 } else if (localName.equals("forecast_date")) {
79 } else if (localName.equals("current_date_time")) {
80 } else if (localName.equals("unit_system")) {
81 if (dataAttribute.equals("SI"))
82 this.usingSITemperature = true;//设为摄氏度
83 }
84 // SHARED(!) 'Inner' Tags within "<current_conditions>" AND
85 // "<forecast_conditions>"
86 else if (localName.equals("day_of_week")) {
87 if (this.in_current_conditions) {
88 //可扩展
89 } else if (this.in_forecast_conditions) {
90 //可扩展
91 }
92 } else if (localName.equals("icon")) {
93 if (this.in_current_conditions) {
94 //设置获取的天气图标
95 this.setIconURL(dataAttribute);
96 } else if (this.in_forecast_conditions) {
97 //可扩展
98 }
99 } else if (localName.equals("condition")) {
100 if (this.in_current_conditions) {
101 //获取当前天气的情况
102 this.setCurrentCondition(dataAttribute);
103 } else if (this.in_forecast_conditions) {
104 //可扩展
105 }
106 }
107 // 'Inner' Tags within "<current_conditions>"
108 else if (localName.equals("temp_f")) {
109 //this.setCurrentTemp(Integer.parseInt(dataAttribute));
110 } else if (localName.equals("temp_c")) {
111 //设置获取的温度
112 this.setCurrentTemp(Integer.parseInt(dataAttribute));
113 } else if (localName.equals("humidity")) {
114 //设置获取的湿度
115 this.setCurrentHum(dataAttribute);
116 } else if (localName.equals("wind_condition")) {
117 //可扩展
118 }
119 // 'Inner' Tags within "<forecast_conditions>"
120 else if (localName.equals("low")) {
121 int temp = Integer.parseInt(dataAttribute);
122 if (this.usingSITemperature) {
123 //可扩展
124 } else {
125 //可扩展
126 }
127 } else if (localName.equals("high")) {
128 //int temp = Integer.parseInt(dataAttribute);
129 if (this.usingSITemperature) {
130 //可扩展
131 } else {
132 //可扩展
133 }
134 }
135 }
136 }
137
138 //单元格解析完后调用
139 public void endElement(String namespaceURI, String localName, String qName)
140 throws SAXException {
141 if (localName.equals("forecast_information")) {
142 this.setIn_forecast_information(false);
143 } else if (localName.equals("current_conditions")) {
144 this.in_current_conditions = false;
145 } else if (localName.equals("forecast_conditions")) {
146 this.in_forecast_conditions = false;
147 }
148 }
149
150
151 public void characters(char ch[], int start, int length) {
152 /*
153 * 可扩展 <element>characters</element>
154 */
155 }
156 public void setIn_forecast_information(boolean in_forecast_information) {
157 this.in_forecast_information = in_forecast_information;
158 }
159 public boolean isIn_forecast_information() {
160 return in_forecast_information;
161 }
162 }

 

2.这里是界面实现的主类,主要是获取一个URL,并且调用上面的类进行解析,得到解析对象值并赋值到指定的控件中。

CurrentWeather
public class CurrentWeather extends Activity {
/** Called when the activity is first created. */

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建一个Button对象
Button submit = (Button) findViewById(R.id.btn);
//设置监听
submit.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {
/*从google上获得图标*/
try {
/*获取用户输入的城市名称 hubei,china*/
String city = ((EditText) findViewById(R.id.input))
.getText().toString();

/*组成URL字符串*/
//中文:http://www.google.com/ig/api?hl=zh-cn&weather=
//英文:http://www.google.com/ig/api?weather=
String queryString = "http://www.google.com/ig/api?weather="
+ city;
/*将可能的空格替换为"%20"*/
URL aURL = new URL(queryString.replace("", "%20"));

/* 从SAXParserFactory获取SAXParser*/
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();

/* 从SAXParser得到XMLReader*/
XMLReader xr = sp.getXMLReader();

/*
* 创建GoogleWeatherHandler,以便解析XML内容
*/
//处理器
GoogleWeatherHandler gwh = new GoogleWeatherHandler();
//注册给ContentHandler(处理器)
xr.setContentHandler(gwh);

/* 解析XML文件内容 */
xr.parse(new InputSource(aURL.openStream()) );


TextView tv1 = (TextView)findViewById(R.id.tem);
tv1.setText("温度:" + gwh.getCurrentTemp() + "摄氏度");

TextView tv2 = (TextView)findViewById(R.id.weather);
tv2.setText(gwh.getCurrentCondition());

TextView tv3 = (TextView)findViewById(R.id.hum);
tv3.setText(""+ gwh.getCurrentHum() );

//网络访问,通过URL请求获取内容
URL iconURL = new URL("http://www.google.com"+ gwh.getIconURL());
//开放一个连接,生成URLConnection对象
URLConnection conn = iconURL.openConnection();
conn.connect();
//获取输入流
InputStream is = conn.getInputStream();
//生成输入缓冲流
BufferedInputStream bis = new BufferedInputStream(is);
//设置icon
ImageView iv = (ImageView)findViewById(R.id.iconOfWeather);
Bitmap bm = null;
//位图加工厂加工获取(decodeStream方法)的数据流来生成Bitmap(位图)
bm = BitmapFactory.decodeStream(bis);
//显示图片
iv.setImageBitmap(bm);
//关闭流
bis.close();
is.close();
} catch (Exception e) {
Log.e("error",e.toString());
}
}//end of onClick
});//end of SetClick
}
}

 

 3.XML的布局

main
 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent"
6 >
7 <LinearLayout
8 android:orientation="horizontal"
9 android:layout_width="fill_parent"
10 android:layout_height="wrap_content"
11 >
12 <EditText
13 android:id="@+id/input"
14 android:layout_width="wrap_content"
15 android:layout_height="wrap_content"
16 android:layout_weight="1"/>
17 <Button
18 android:id="@+id/btn"
19 android:layout_width="wrap_content"
20 android:layout_height="wrap_content"
21 android:text="提交"/>
22 </LinearLayout>
23 <ImageView
24 android:id="@+id/iconOfWeather"
25 android:layout_width="wrap_content"
26 android:layout_height="wrap_content">
27 </ImageView>
28 <LinearLayout
29 android:orientation="vertical"
30 android:layout_width="fill_parent"
31 android:layout_height="wrap_content"
32 >
33 <TextView
34 android:id="@+id/weather"
35 android:layout_width="wrap_content"
36 android:layout_height="wrap_content"
37
38 />
39 <TextView
40 android:id="@+id/tem"
41 android:layout_width="wrap_content"
42 android:layout_height="wrap_content"
43 />
44 <TextView
45 android:id="@+id/hum"
46 android:layout_width="wrap_content"
47 android:layout_height="wrap_content"
48 />
49 </LinearLayout>
50 </LinearLayout>

 

 4.别忘了在AndroidManifest.xml中配置权限

<uses-permission android:name="android.permission.INTERNET" />

posted @ 2011-11-15 11:49  WangWeiDa  阅读(637)  评论(0编辑  收藏  举报