博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用Google Weather API获取天气预报,中文支持
阅读量:5076 次
发布时间:2019-06-12

本文共 4704 字,大约阅读时间需要 15 分钟。

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

前端页面:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="GoogleWeatherAPI.aspx.cs" Inherits="EnhancementLot4.Tests.GoogleWeatherAPI" %> 

后端代码:

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 + "City : " + nodeList1.Item(0).SelectSingleNode("city").Attributes["data"].InnerText + ""; XmlNodeList nodeList = root.SelectNodes("weather/current_conditions"); weather = weather + "
" + nodeList.Item(0).SelectSingleNode("temp_c").Attributes["data"].InnerText + " °C | " + nodeList.Item(0).SelectSingleNode("temp_f").Attributes["data"].InnerText + " °F
"; weather = weather + "Current: " + 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 + "
" + nod.SelectSingleNode("day_of_week").Attributes["data"].InnerText + ""; weather = weather + "\"""; weather = weather + nod.SelectSingleNode("low").Attributes["data"].InnerText + "°F | "; weather = weather + nod.SelectSingleNode("high").Attributes["data"].InnerText + "°F"; } weather = weather + "
"; } 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:

中文:

 

参考:

转载于:https://www.cnblogs.com/Fred_Xu/archive/2012/03/23/using-google-weather-api-to-get-weather-and-time-via-csharp.html

你可能感兴趣的文章
url查询参数解析
查看>>
http://coolshell.cn/articles/10910.html
查看>>
[转]jsbsim基础概念
查看>>
mysql编码问题
查看>>
决策树与规则引擎
查看>>
nekohtml转换html时标签变大写的问题
查看>>
Python随笔1
查看>>
夺命雷公狗---Redis---6-案例操作1(注册-登录)
查看>>
IE8不显示图片alt了.
查看>>
C++中声明与定义的区别
查看>>
U3d PlayerPrefs
查看>>
《Linux命令行与shell脚本编程大全》 第八章管理文件系统
查看>>
前端css常用的选择小汇
查看>>
python之路-Memcache
查看>>
Hadoop Streaming框架使用(一)
查看>>
Prometheus Operator - 每天5分钟玩转 Docker 容器技术(177)
查看>>
vue学习记录③(路由)
查看>>
psutil模块
查看>>
Jzoj4743 积木
查看>>
工厂模式
查看>>