注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Equinox's Blog.

My Naughty Corner

 
 
 

日志

 
 

本周小作:GoogleWeatherAPI類For C#  

2010-03-13 12:02:48|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

SourceCode見後面.

類名稱GoogleWeatherAPI.

 

構造函數:

GoogleWeatherAPI():

創建新的GoogleWeatherAPI實例.

 

GoogleWeatherAPI(String URLS):

使用指定的URLString初始化新GoogleWeatherAPI實例.

Ex: new GoogleWeatherAPI("http://www.google.com/ig/api?weather=Canton");

 

GoogleWeatherAPI(String city,String language):

使用指定的城市和顯示的語言初始化GoogleWeatherAPI實例,language可為null.

Ex: new GoogleWeatherAPI("Canton","zh-cn");

 

方法:

bool Get():

通過URLString連接GoogleWeather服務器獲取天氣預報信息, 結果返回到Hashtable ForecastInformation,ArrayList ForecastConditions,Hashtable CurrentConditions中.

返回:是否成功獲取GoogleWeather信息.

異常:

ArgumentNullException:沒有設定實例的URLString.

WebException:連接失敗所造成的異常.

 

void SetArguments(String city,String language):

使用指定的City和Language設定實例的URLString,language可為null.

 

屬性:

Hashtable ForecastInformation:

聲明:public System.Collections.Hashtable ForecastInformation {get;}

返回一個Hashtable,裏面包括本次查詢的信息.

使用請先調用void Get()方法,若之前未調用Get()方法則返回null.

 

Hashtable CurrentConditions:

聲明:public System.Collections.Hashtable CurrentConditions {get;}

返回一個Hashtable,裏面包括當前的天氣信息.

使用請先調用void Get()方法,若之前未調用Get()方法則返回null.

 

ArrayList ForecastConditions:

聲明:public System.Collections.ArrayList ForecastConditions {get;}

返回一個包括從今天到後天天氣預報信息的ArrayList,其中的元素是天氣預報信息的Hashtable.

使用請先調用void Get()方法,若之前未調用Get()方法則返回null.

 

int Timeout:

聲明:public int Timeout {get; set;}

設置或獲取連接的超時時間,默認值為20000.

 

String URLString:

聲明:public String URLString {get; set;}

設置或獲取連接的URL.

 

String XMLCode:

聲明:public String XMLCode {get;}

返回本次查詢所獲得的XML代碼字符串.

使用請先調用void Get()方法,若之前未調用Get()方法則返回null.

 

SourceCode:

 

//   GoogleWeatherAPI.cs

//   Created by Equinox on 10-3-13.

//   Copyright 2010. All rights reserved.

 

using System;

using System.Collections.Generic;

using System.Text;

using System.Xml;

using System.Net;

using System.IO;

 

/// <summary>

/// 在C#中使用GoogleWeatherAPI的類

/// </summary>

    class GoogleWeatherAPI

    {

        private int timeout;

        private String URLStr;

        private System.Collections.Hashtable fc;

        private System.Collections.ArrayList ht;

        private System.Collections.Hashtable ct;

        private String xmlc;

        /// <summary>

        /// 返回本次查詢所獲得的XML代碼字符串.

        ///使用請先調用void Get()方法,若之前未調用Get()方法則返回null.

        /// </summary>

        public String XMLCode

        {

            get

            {

                return xmlc;

            }

        }

        /// <summary>

        /// 設置或獲取連接的URL.

        /// </summary>

        public String URLString

        {

            get

            {

                return URLStr;

            }

            set

            {

                URLStr = value;

            }

        }

        /// <summary>

        /// 設置或獲取連接的超時時間,默認值為20000.

        /// </summary>

        public int Timeout

        {

            get

            {

                return timeout;

            }

            set

            {

                timeout = value;

            }

        }

        /// <summary>

        /// 返回一個Hashtable,裏面包括本次查詢的信息.

        /// 使用請先調用void Get()方法,若之前未調用Get()方法則返回null.

        /// </summary>

        public System.Collections.Hashtable ForecastInformation

        {

            get

            {

                return fc;

            }

        }

        /// <summary>

        /// 返回一個包括從今天到後天天氣預報信息的ArrayList,其中的元素是天氣預報信息的Hashtable.

        ///使用請先調用void Get()方法,若之前未調用Get()方法則返回null.

        /// </summary>

        public System.Collections.ArrayList ForecastConditions

        {

            get

            {

                return ht;

            }

        }

        /// <summary>

        /// 返回一個Hashtable,裏面包括當前的天氣信息.

        ///使用請先調用void Get()方法,若之前未調用Get()方法則返回null.

        /// </summary>

        public System.Collections.Hashtable CurrentConditions

        {

            get

            {

                return ct;

            }

        }

        private string GetSourceTextByUrl(string url)

        {

            WebRequest request = WebRequest.Create(url);

            request.Timeout=timeout;

            WebResponse response = request.GetResponse();

                Stream resStream = response.GetResponseStream();

                StreamReader sr = new StreamReader(resStream, Encoding.Default);

                return sr.ReadToEnd();

        }

        /// <summary>

        /// 通過URLString連接GoogleWeather服務器獲取天氣預報信息, 結果返回到Hashtable ForecastInformation,ArrayList ForecastConditions,Hashtable CurrentConditions中.

        /// </summary>

        /// <returns>是否成功獲取GoogleWeather信息.</returns>

        /// <exception cref="ArgumentNullException">沒有設定實例的URLString.</exception>

        /// <exception cref="WebException">連接失敗所造成的異常.</exception>

        public bool Get()

        {

            if (URLStr == null)

            {

                ArgumentNullException e = new ArgumentNullException("URLString");

                throw e;

            }

                        String nodename = "";

            xmlc = GetSourceTextByUrl(URLStr);

            fc = new System.Collections.Hashtable();

            ht = new System.Collections.ArrayList();

            ct = new System.Collections.Hashtable();

            XmlTextReader reader = new XmlTextReader(xmlc, XmlNodeType.Document, new XmlParserContext(null, null, null, XmlSpace.Default, Encoding.Default));

            int fcc2 = 0;

            while (reader.Read())

            {

                if ((reader.Name == "forecast_information" || reader.Name == "current_conditions") && reader.NodeType == XmlNodeType.Element)

                {

                    nodename = reader.Name;

                    continue;

                }

                if (reader.Name == "forecast_conditions" && reader.NodeType == XmlNodeType.Element)

                {

                    ht.Add(new System.Collections.Hashtable());

                    nodename = reader.Name;

                    continue;

                }

                if (nodename != "" && reader.NodeType == XmlNodeType.Element)

                {

                    if (reader.AttributeCount == 0) continue;

                    if (nodename == "forecast_information")

                    {

                        fc.Add(reader.Name, reader[0]);

                    }

                    if (nodename == "current_conditions")

                    {

                        ct.Add(reader.Name, reader[0]);

                    }

                    else if (nodename == "forecast_conditions")

                    {

                        ((System.Collections.Hashtable)ht[fcc2]).Add(reader.Name, reader[0]);

                    }

                    continue;

                }

                if (nodename == "forecast_conditions" && reader.NodeType == XmlNodeType.EndElement)

                {

                    fcc2++;

                }

            }

            reader.Close();

            return true;

        }

        /// <summary>

        /// 創建新的GoogleWeatherAPI實例.

        /// </summary>

        public GoogleWeatherAPI(){

            timeout = 20000;

        }

        /// <summary>

        /// 使用指定的City和Language設定實例的URLString.

        /// </summary>

        /// <param name="city">指定的City</param>

        /// <param name="language">指定的Language,可為null.</param>

        public void SetArguments(String city,String language)

        {

            URLStr=MakeWeatherURL(city,language);

        }

        /// <summary>

        /// 使用指定的城市和顯示的語言初始化GoogleWeatherAPI實例.

        /// </summary>

        /// <param name="city">指定的城市</param>

        /// <param name="language">指定要顯示的語言,可為null</param>

        /// <example>GoogleWeatherAPI("Canton","zh-cn")</example>

        public GoogleWeatherAPI(String city,String language) {

            timeout = 20000;

            SetArguments(city,language);

        }

        /// <summary>

        /// 使用指定的URLString初始化新GoogleWeatherAPI實例.

        /// </summary>

        /// <param name="URLS">指定的URLString</param>

        /// <example>GoogleWeatherAPI("http://www.google.com/ig/api?weather=Canton")</example>

        public GoogleWeatherAPI(String URLS)

        {

            timeout = 20000;

            URLStr = URLS;

        }

        private String MakeWeatherURL(String city, String language)

        {

            if (city == null) return null;

            String turl = "http://www.google.com/ig/api?weather=" + city;

            if (language != null)

            {

                turl +="&hl=" + language;

            }

            return turl;

        }

    }

 

 

  评论这张
 
阅读(322)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017