google weather API and XML.docx

上传人:b****8 文档编号:9061068 上传时间:2023-02-03 格式:DOCX 页数:15 大小:292.10KB
下载 相关 举报
google weather API and XML.docx_第1页
第1页 / 共15页
google weather API and XML.docx_第2页
第2页 / 共15页
google weather API and XML.docx_第3页
第3页 / 共15页
google weather API and XML.docx_第4页
第4页 / 共15页
google weather API and XML.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

google weather API and XML.docx

《google weather API and XML.docx》由会员分享,可在线阅读,更多相关《google weather API and XML.docx(15页珍藏版)》请在冰豆网上搜索。

google weather API and XML.docx

googleweatherAPIandXML

目标:

你将学到如何通过SAXParser类解析调用GoogleWeatherAPI接口返回的XML结果

环境:

SDK1.1

内容:

调用GoogleWeatherAPI

通过WEB下载数据

通过SAXParser解析XML文件(数据流)

创建自定义界面(继承LinearLayout)

难度:

3.5/5

笔者用2小时写代码,其中半个小时是用于xml-layout,这再次显示了Android-Platform平台的强大和灵活

问题/疑难:

结果:

ScreenshottakenwithSDK-versionm3:

描述:

在这个教程中,我们将使用Google(iGoogle)WeatherAPI,我们将简单地调用如下样式的URL

引用:

//Style:

//WorkingExamples(Note:

that""(space)hastobereplacedwithitshtml-expression"%20"

//Yourbrowsermanagesthatconversionautomatically)

这里使用googleweatherAPI三种调用方法中的第三种,使用城市名称法

扩展说明:

(译者)

GoogleWeatherAPI调用说明

1.邮政编码法:

(支持美国地区)

  

  (94043为山景城,美国加州的邮政编码)

2.经纬度坐标作法:

  

  (30670000,104019996为成都,中国大陆的经纬度坐标)

3.城市名称法:

  

当查询的城市能找到时,返回的数据样式如下:

XML:

   

      

         

--Someinnertagscontainingdataaboutthecityfound,timeandunit-stuff-->

         

         

         

         

         

         

50:

00+0000"/>

         

      

      

         

--Someinnertagscontainingdataofcurrentweather-->

         

         

         

         

93%"/>

         

         

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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 外语学习 > 英语考试

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1