Nat1mph"/>
--Someinnertagscontainingdataaboutfutureweather-->
--Someinnertagscontainingdataaboutfutureweather-->
--Anothersetasabove-->
--Anothersetasabove-->
如果你访问的城市不存在时,例如:
它将返回一个提示错误的XML,格式如下:
XML:
1)由于我们需要解析返回的XML信息,因此我们需要选择一个XML解析工具(XML-parse)
我(原作者)决定选择使用SAX-Parser,SAX是因简单易用的XML-API而著称.所以它正合适我们使用。
(注:
参看相关资料)
通过解析转换,它可以把weatherapi返回的XML数据转成如下的对象:
Java:
publicclassWeatherSet{
//===========================================================
//Fields
//===========================================================
privateWeatherCurrentConditionmyCurrentCondition=null;
privateArrayListmyForecastConditions=
newArrayList(4);
//===========================================================
//Getter&Setter
//===========================================================
//Getter&Setterforfieldsabove...
}
它包括了两个子成员:
WeatherCurrentCondition,WeatherForecastInfoSet
WeatherCurrentCondition:
包含了XML数据中current_conditions节点的标签。
WeatherForecastInfoSet:
包含了XML数据中forecast_conditions节点的标签。
关于XML解析部分就在这里结束了,其它信息可以看实际代码。
2)让我们看一下它的布局,这个布局相对以前我们做的来说会更复杂些。
下面是它的关系
我使用如些多的tablelayouts,是因为tablelayout能更容易伸展它的子视窗。
使用小图标表示天气,使用文本显示温度,他们共同组成自定义视图(SingleWeatherInfoView),它继承于LinearLayout,如下图
通过继承LinearLayout,我们得到设置android:
orientation-Attributeto="horizontal"或是"vertical"的属性。
你可以出图片的功能,在视图中心的图片SingleWeatherInfoView是当天的天气。
在这下面的四个图片SingleWeatherInfoView依次显示的是之后四天的天气。
在此又一次的显示了面向对象编程的是最好的。
如何创建自定义视图,可以查看实际代码。
3)到此90%的工作已经完成,最后的事情是把他们组合起来,我们使用提交按钮的OnClickListener监听事件,当点击后,发生如下事情:
●从输入框中取出用户所输入的数据当成调成googleweatherapi
●创建一个SAXParser的XML解析对象,解析调用googleweatherapi后返回的数据
●显示天气预报信息,包括当天和之后四天的天气。
添加如下代码:
Andthisisthecorrespondingcode:
Java:
Buttoncmd_submit=(Button)findViewById(R.id.cmd_submit);
cmd_submit.setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewarg0){
URLurl;
try{
/*GetwhatusertypedtotheEditText.*/
StringcityParamString=
((EditText)findViewById(R.id.edit_input))
.getText().toString();
StringqueryString=
"
+cityParamString;
/*ReplaceblankswithHTML-Equivalent.*/
url=newURL(queryString.replace("","%20"));
/*GetaSAXParserfromtheSAXPArserFactory.*/
SAXParserFactoryspf=SAXParserFactory.newInstance();
SAXParsersp=spf.newSAXParser();
/*GettheXMLReaderoftheSAXParserwecreated.*/
XMLReaderxr=sp.getXMLReader();
/*CreateanewContentHandlerandapplyittotheXML-Reader*/
GoogleWeatherHandlergwh=newGoogleWeatherHandler();
xr.setContentHandler(gwh);
/*Parsethexml-dataourURL-callreturned.*/
xr.parse(newInputSource(url.openStream()));
/*OurHandlernowprovidestheparsedweather-datatous.*/
WeatherSetws=gwh.getWeatherSet();
/*UpdatetheSingleWeatherInfoViewwiththeparseddata.*/
updateWeatherInfoView(R.id.weather_today,ws.getWeatherCurrentCondition());
updateWeatherInfoView(R.id.weather_1,ws.getWeatherForecastConditions().get(0));
updateWeatherInfoView(R.id.weather_2,ws.getWeatherForecastConditions().get
(1));
updateWeatherInfoView(R.id.weather_3,ws.getWeatherForecastConditions().get
(2));
updateWeatherInfoView(R.id.weather_4,ws.getWeatherForecastConditions().get(3));
}catch(Exceptione){
resetWeatherInfoViews();
Log.e(DEBUG_TAG,"WeatherQueryError",e);
}
}
});
如何显示网络图片?
通过IE访问:
SingleWeatherInfoView类中setWeatherIcon
新建工程把下面代码加入工程
privateImageViewmyWeatherImageView=null;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myWeatherImageView=(ImageView)findViewById(R.id.webimg);
this.myWeatherImageView.setPadding(10,5,5,5);
URLurl;
try{
url=newURL("
setWeatherIcon(url);
}catch(MalformedURLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
publicvoidsetWeatherIcon(URLaURL){
try{
URLConnectionconn=aURL.openConnection();
conn.connect();
InputStreamis=conn.getInputStream();
BufferedInputStreambis=newBufferedInputStream(is);
Bitmapbm=BitmapFactory.decodeStream(bis);
bis.close();
is.close();
this.myWeatherImageView.setImageBitmap(bm);
}catch(Exceptione){
}
}
在main.xml中添加:
android:
id="@+id/webimg"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
layout_gravity="center"
>
注意:
在AndroidManifest.xml文中添加
name="android.permission.INTERNET">
才能访问网络.
运行显示:
工程结构:
WeatherForecast:
视图,程序主入口
GoogleWeatherHandler:
googleweather处理器,主要是解析XML文件
WeatherSet:
是XML标签集,其中包括成员WeatherCurrentCondition,WeatherForecastCondition
SingleWeatherInfoView:
以下视图
关于译者:
向上是一名C语言开发人员,现从事MTK手机开发,有三年C++,C终端开发经验。
现看好android将来的发展,加到机器人大家庭,努力学习,为社区贡献力量。
坚持向上人生路。
参考资料:
http:
//www.anddev.org/android_weather_forecast_-_google_weather_api_-_full_source-t361.html
从mapbar地图中取出来的按“城市名字,经度,纬度,
附:
全国城市名字,经度,纬度
全国,105.99841,39.00865,北京市,116.38689,39.90578,天津市,117.20264,39.15079,塘沽,117.66769,39.03314,大港区,117.42982,38.84287,上海市,121.45611,31.21475,重庆市,106.52435,29.52496,广东省,113.24316,23.13135,广州市,113.24316,23.13135,清远市,113.02238,23.71833,佛山市,113.0961,23.02553,深圳市,114.10638,22.55189,湛江市,110.39712,21.19755,肇庆市,112.46474,23.07627,中山市,113.36877,22.5251,东莞市,113.74772,23.04402,韶关市,113.60937,24.81176,汕头市,116.68011,23.35566,顺德市,113.26533,22.81713,珠海市,113.54457,22.245