代码改变世界

使用Google Weather API获取天气预报,中文支持

2012-03-23 11:48  Fred-Xu  阅读(2051)  评论(0编辑  收藏  举报

这里我们使用asp.net来获取当地天气、时间信息

前端页面:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="GoogleWeatherAPI.aspx.cs" Inherits="EnhancementLot4.Tests.GoogleWeatherAPI" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:Literal ID="ltlWeather" runat="server"></asp:Literal>
</asp:Content>

后端代码:

protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Request.QueryString["location"]))
ltlWeather.Text = GetWeather(Request.QueryString["location"]);
}

private string GetWeather(string location)
{
HttpWebRequest GoogleRequest;
HttpWebResponse GoogleResponse = null;
XmlDocument GoogleXMLdoc = null;
string weather = string.Empty;

try
{
GoogleRequest = (HttpWebRequest)WebRequest.Create("http://www.google.com/ig/api?output=xml&hl=zh-cn&country=cn&weather=" + string.Format(location));
GoogleResponse = (HttpWebResponse)GoogleRequest.GetResponse();
GoogleXMLdoc = new XmlDocument();
GoogleXMLdoc.XmlResolver = null;
GoogleXMLdoc.Load(GoogleResponse.GetResponseStream());
XmlNode root = GoogleXMLdoc.DocumentElement;
XmlNodeList nodeList1 = root.SelectNodes("weather/forecast_information");
weather = weather + "<b>City : " + nodeList1.Item(0).SelectSingleNode("city").Attributes["data"].InnerText + "</b>";
XmlNodeList nodeList = root.SelectNodes("weather/current_conditions");

weather = weather + "<table class=\"bordered\" cellpadding=\"5\"><tbody><tr><td><b><big><nobr>" + nodeList.Item(0).SelectSingleNode("temp_c").Attributes["data"].InnerText + " °C | " + nodeList.Item(0).SelectSingleNode("temp_f").Attributes["data"].InnerText + " °F</nobr></big></b>";
weather = weather + "<b>Current:</b> " + nodeList.Item(0).SelectSingleNode("condition").Attributes["data"].InnerText + "";
weather = weather + " " + nodeList.Item(0).SelectSingleNode("wind_condition").Attributes["data"].InnerText + "";
weather = weather + " " + nodeList.Item(0).SelectSingleNode("humidity").Attributes["data"].InnerText;
nodeList = root.SelectNodes("descendant::weather/forecast_conditions");
foreach (XmlNode nod in nodeList)
{
weather = weather + "</td><td align=\"center\">" + nod.SelectSingleNode("day_of_week").Attributes["data"].InnerText + "";
weather = weather + "<img src=\"http://www.google.com" + nod.SelectSingleNode("icon").Attributes["data"].InnerText + "\" alt=\"" + nod.SelectSingleNode("condition").Attributes["data"].InnerText + "\">";
weather = weather + nod.SelectSingleNode("low").Attributes["data"].InnerText + "°F | ";
weather = weather + nod.SelectSingleNode("high").Attributes["data"].InnerText + "°F";
}
weather = weather + "</td></tr></tbody></table>";
}
catch (System.Exception ex)
{
weather = ex.Message;
}
finally
{
GoogleResponse.Close();
}

return weather;
}

注意这里我们是通过http://www.google.com/ig/api?output=xml&hl=zh-cn&country=cn来获取中文显示的天气信息,上面的代码运行时会报错:

Invalid character in the given encoding

其原因是HttpWebRequest得到的Response是GB2312编码,而默认XmlDocument是UTF-8编码的,所以会报错。

解决很简单,只要将Response的Stream改为GB2312编码即可:

                GoogleRequest = (HttpWebRequest)WebRequest.Create("http://www.google.com/ig/api?output=xml&hl=zh-cn&country=cn&weather=" + string.Format(location));
GoogleResponse = (HttpWebResponse)GoogleRequest.GetResponse();
GoogleXMLdoc = new XmlDocument();
GoogleXMLdoc.XmlResolver = null;
StreamReader reader = new StreamReader(GoogleResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("GB2312"));
GoogleXMLdoc.Load(reader);
XmlNode root = GoogleXMLdoc.DocumentElement;

另附上英文显示的XML:

<xml_api_reply version="1">
<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
<forecast_information>
<city data="Shanghai, Shanghai"/>
<postal_code data="shanghai"/>
<latitude_e6 data=""/>
<longitude_e6 data=""/>
<forecast_date data="2012-03-23"/>
<current_date_time data="2012-03-23 16:30:00 +0000"/>
<unit_system data="US"/>
</forecast_information>
<current_conditions>
<condition data="Light rain"/>
<temp_f data="45"/>
<temp_c data="7"/>
<humidity data="Humidity: 93%"/>
<icon data="/ig/images/weather/mist.gif"/>
<wind_condition data="Wind: W at 16 mph"/>
</current_conditions>
<forecast_conditions>
<day_of_week data="Fri"/>
<low data="39"/>
<high data="55"/>
<icon data="/ig/images/weather/rain.gif"/>
<condition data="Rain"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="Sat"/>
<low data="39"/>
<high data="55"/>
<icon data="/ig/images/weather/mostly_sunny.gif"/>
<condition data="Mostly Sunny"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="Sun"/>
<low data="37"/>
<high data="63"/>
<icon data="/ig/images/weather/sunny.gif"/>
<condition data="Clear"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="Mon"/>
<low data="43"/>
<high data="57"/>
<icon data="/ig/images/weather/sunny.gif"/>
<condition data="Clear"/>
</forecast_conditions>
</weather>
</xml_api_reply

中文:

<xml_api_reply version="1">
<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
<forecast_information>
<city data="Shanghai, Shanghai"/>
<postal_code data="shanghai"/>
<latitude_e6 data=""/>
<longitude_e6 data=""/>
<forecast_date data="2012-03-23"/>
<current_date_time data="2012-03-23 16:30:00 +0000"/>
<unit_system data="SI"/>
</forecast_information>
<current_conditions>
<condition data="小雨"/>
<temp_f data="45"/>
<temp_c data="7"/>
<humidity data="湿度: 93%"/>
<icon data="/ig/images/weather/cn_lightrain.gif"/>
<wind_condition data="风向: 西、风速:7 米/秒"/>
</current_conditions>
<forecast_conditions>
<day_of_week data="周五"/>
<low data="4"/>
<high data="13"/>
<icon data="/ig/images/weather/cn_heavyrain.gif"/>
<condition data="雨"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="周六"/>
<low data="4"/>
<high data="13"/>
<icon data="/ig/images/weather/mostly_sunny.gif"/>
<condition data="以晴为主"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="周日"/>
<low data="3"/>
<high data="17"/>
<icon data="/ig/images/weather/sunny.gif"/>
<condition data="晴"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="周一"/>
<low data="6"/>
<high data="14"/>
<icon data="/ig/images/weather/sunny.gif"/>
<condition data="晴"/>
</forecast_conditions>
</weather>
</xml_api_reply>

 

参考:http://www.yaosansi.com/post/1366.html

http://www.aspmvcnet.com/asp-net-mvc/google-weather-api-for-asp-net-mvc.aspx