1 新建一个项目
2 MainActivity.java中的代码
package basic.android.xml.sax; |
02 |
03 | import android.app.Activity; |
04 | import android.os.Bundle; |
05 | import android.view.View; |
06 | import android.widget.Button; |
07 | import android.widget.TextView; |
08 |
09 | public class MainActivity extends Activity { |
10 |
11 | @Override |
12 | public void onCreate(Bundle savedInstanceState) { |
13 | super .onCreate(savedInstanceState); |
14 | setContentView(R.layout.main); |
15 |
16 | //定义UI组件 |
17 | Button b1 = (Button) findViewById(R.id.button1); |
18 | final TextView tv1 = (TextView) findViewById(R.id.textView1); |
19 |
20 | //为按钮绑定监听器 |
21 | b1.setOnClickListener( new View.OnClickListener() { |
22 | @Override |
23 | public void onClick(View arg0) { |
24 | //定义一个查询Google天气的字符串,后面的经纬度我写死成郑州的坐标了,你懂的 |
25 | String googleWeatherUrl = "http://www.google.com/ig/api?hl=zh-cn&ie=utf-8&weather=,,,34720001,113650001" ; |
26 | //定义了一个HttpClientConnector工具类用来把google天气预报返回的XML信息存储在一个字符串里,这里可能会有聪明的同学说,你已经把整个xml都读回来了,还扯什么读一半就可以退出的话,这里要说明的是google Weather API很蛋疼,直接用sax解析会出错,所以只能先完整读回来 |
27 | String googleWeatherString = HttpClientConnector.getStringByUrl(googleWeatherUrl); |
28 | //定义一个SAX Parse对象把xml的字符串解析成我们要的 明日天气信息Bean |
29 | TomorrowWeatherVO tomorrowWeatherVO = TomorrowWeatherParse.parse(googleWeatherString); |
30 | //显示天气信息 |
31 | if (tomorrowWeatherVO!= null ){ |
32 | tv1.setText( "明日天气情况:" + tomorrowWeatherVO.getCondition() + " 最高气温:" + tomorrowWeatherVO.getHigh() |
33 | + " 最低气温:" + tomorrowWeatherVO.getLow()); |
34 | } |
35 | } |
36 | }); |
37 | } |
38 |
39 | } |
3、上面使用的HttpClientConnector工具类代码如下:
01 | package basic.android.xml.sax; |
02 |
03 | import org.apache.http.client.ResponseHandler; |
04 | import org.apache.http.client.methods.HttpGet; |
05 | import org.apache.http.impl.client.BasicResponseHandler; |
06 | import org.apache.http.impl.client.DefaultHttpClient; |
07 |
08 | import android.util.Log; |
09 |
10 | public class HttpClientConnector { |
11 |
12 | static String getStringByUrl(String url) { |
13 |
14 | String outputString = "" ; |
15 |
16 | // DefaultHttpClient |
17 | DefaultHttpClient httpclient = new DefaultHttpClient(); |
18 | // HttpGet |
19 | HttpGet httpget = new HttpGet(url); |
20 | // ResponseHandler |
21 | ResponseHandler<string> responseHandler = new BasicResponseHandler(); |
22 |
23 | try { |
24 | outputString = httpclient.execute(httpget, responseHandler); |
25 | Log.i( "yao" , "连接成功" ); |
26 | } catch (Exception e) { |
27 | Log.i( "yao" , "连接失败" ); |
28 | e.printStackTrace(); |
29 | } |
30 | httpclient.getConnectionManager().shutdown(); |
31 | return outputString; |
32 |
33 | } |
34 |
35 | }</string> |
4、SAX解析器 TomorrowWeatherParse.java的代码如下:
01 | package basic.android.xml.sax; |
02 |
03 | import java.io.IOException; |
04 | import java.io.StringReader; |
05 |
06 | import javax.xml.parsers.ParserConfigurationException; |
07 | import javax.xml.parsers.SAXParserFactory; |
08 |
09 | import org.xml.sax.InputSource; |
10 | import org.xml.sax.SAXException; |
11 | import org.xml.sax.XMLReader; |
12 |
13 | public class TomorrowWeatherParse { |
14 |
15 | // 解析天气预报字符串成一个天气信息对象 |
16 | public static TomorrowWeatherVO parse(String googleWeatherString) { |
17 |
18 | SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); |
19 |
20 | TomorrowWeatherVO tomorrowWeatherVO = new TomorrowWeatherVO(); |
21 |
22 | try { |
23 | XMLReader xmlReader = saxParserFactory.newSAXParser().getXMLReader(); |
24 | WeatherXMLHandler handler = new WeatherXMLHandler(tomorrowWeatherVO); |
25 | xmlReader.setContentHandler(handler); |
26 |
27 | xmlReader.parse( new InputSource( new StringReader(googleWeatherString))); |
28 |
29 | } catch (SAXException e) { |
30 | e.printStackTrace(); |
31 | } catch (ParserConfigurationException e) { |
32 | e.printStackTrace(); |
33 | } catch (IOException e) { |
34 | e.printStackTrace(); |
35 | } |
36 |
37 | return tomorrowWeatherVO; |
38 |
39 | } |
40 |
41 | } |
5、TomorrowWeatherParse.java 中使用到的内容处理器 WeatherXMLHandler.java的代码如下:
01 | package basic.android.xml.sax; |
02 |
03 | import org.xml.sax.Attributes; |
04 | import org.xml.sax.SAXException; |
05 | import org.xml.sax.helpers.DefaultHandler; |
06 | import android.util.Log; |
07 |
08 | public class WeatherXMLHandler extends DefaultHandler { |
09 |
10 | // 明日天气预报Bean |
11 | TomorrowWeatherVO tomorrowWeatherVO; |
12 |
13 | // 记录出现次数 |
14 | int findCount = 0 ; |
15 |
16 | // 默认构造方法 |
17 | public WeatherXMLHandler() { |
18 | super (); |
19 | } |
20 |
21 | // 构造方法 |
22 | public WeatherXMLHandler(TomorrowWeatherVO tomorrowWeatherVO) { |
23 | this .tomorrowWeatherVO = tomorrowWeatherVO; |
24 | } |
25 |
26 | /* |
27 | * 文档结束时触发 |
28 | */ |
29 | @Override |
30 | public void endDocument() throws SAXException { |
31 | Log.i("yao", "文档解析结束"); |
32 | super.endDocument(); |
33 | } |
34 |
35 | /* |
36 | * 文档开始时触发 |
37 | */ |
38 | @Override |
39 | public void startDocument() throws SAXException { |
40 | Log.i("yao", "文档解析开始"); |
41 | super.startDocument(); |
42 | } |
43 |
44 | /* |
45 | * 元素开始时触发 |
46 | */ |
47 | @Override |
48 | public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { |
49 | Log.i("yao", qName); |
50 | if (qName.equals("forecast_conditions")) { |
51 | findCount++; |
52 | } |
53 | Log.i("yao", "" + findCount); |
54 | if (findCount == 2) { |
55 | if (qName.equals("low")) { |
56 | tomorrowWeatherVO.setLow(attributes.getValue("data")); |
57 | } |
58 | if (qName.equals("high")) { |
59 | tomorrowWeatherVO.setHigh(attributes.getValue("data")); |
60 | } |
61 | if (qName.equals("icon")) { |
62 | tomorrowWeatherVO.setIcon(attributes.getValue("data")); |
63 | } |
64 | if (qName.equals("condition")) { |
65 | tomorrowWeatherVO.setCondition(attributes.getValue("data")); |
66 | } |
67 | } |
68 | super.startElement(uri, localName, qName, attributes); |
69 | } |
70 |
71 | /* |
72 | * 元素结束时触发 |
73 | */ |
74 | @Override |
75 | public void endElement(String uri, String localName, String qName) throws SAXException { |
76 | Log.i("yao", "元素解析结束"); |
77 | super.endElement(uri, localName, qName); |
78 | } |
79 |
80 | /* |
81 | * 读取元素内容 |
82 | */ |
83 | @Override |
84 | public void characters( char [] ch, int start, int length) throws SAXException { |
85 | super .characters(ch, start, length); |
86 | } |
87 |
88 | } |
上面的代码里有好多空方法,是为了让你了解默认的内容处理器DefaultHandler中的常用方法,其中因为google天气xml的特殊结构,让我们没有机会使用一个更常用的方法characters,很是遗憾,大家自己找资料学习吧。
6、最后还有一个,存储明日天气信息的Bean:TomorrowWeatherVO.java
01 | package basic.android.xml.sax; |
02 |
03 | public class TomorrowWeatherVO { |
04 |
05 | String low; |
06 | String high; |
07 | String icon; |
08 | String condition; |
09 | |
10 | |
11 | public String getLow() { |
12 | return low; |
13 | } |
14 | public void setLow(String low) { |
15 | this .low = low; |
16 | } |
17 | public String getHigh() { |
18 | return high; |
19 | } |
20 | public void setHigh(String high) { |
21 | this .high = high; |
22 | } |
23 | public String getIcon() { |
24 | return icon; |
25 | } |
26 | public void setIcon(String icon) { |
27 | this .icon = icon; |
28 | } |
29 | public String getCondition() { |
30 | return condition; |
31 | } |
32 | public void setCondition(String condition) { |
33 | this .condition = condition; |
34 | } |
35 | |
36 | public TomorrowWeatherVO(String low, String high, String icon, |
37 | String condition) { |
38 | super (); |
39 | this .low = low; |
40 | this .high = high; |
41 | this .icon = icon; |
42 | this .condition = condition; |
43 | } |
44 | |
45 | public TomorrowWeatherVO() { |
46 | |
47 | } |
48 | } |
7、照例还是把简陋的布局文件贴出来main.xml
1 | <?xml version= "1.0" encoding= "utf-8" ?> |
2 | <linearlayout android:layout_height= "fill_parent" android:layout_width= "fill_parent" android:orientation= "vertical" xmlns:android= "http://schemas.android.com/apk/res/android" > |
3 | <button android:layout_height= "wrap_content" android:layout_width= "wrap_content" android:id= "@+id/button1" android:text= "获取明天天气情况" > |
4 | </button> |
5 | <textview android:layout_height= "wrap_content" android:layout_width= "wrap_content" android:id= "@+id/textView1" android:text= "" > |
6 | </textview> |
7 | </linearlayout> |
8、最后不要忘了在AndroidManifest.xml中加入 访问互联网的权限:
1 | <uses -permission= "" android:name= "android.permission.INTERNET" ></uses> |
好,我们可以编译并运行程序,查看结果了: