大家好,我是使用谷歌api获取天气更新的新手。这个问题产生了一个错误。。我设置了doctype,但问题没有解决。。请帮我提前谢谢。。。
.aspx文件
<%@ Page Language="C#" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication4.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head id="Head1" runat="server">
<title>Put Your Title Here</title>
<style type="text/css">
#Panel1
{
width:350px;
}
#Panel1 img
{
float:left;
width:100px;
height:100px;
margin-top:10px;
}
#Panel1 p
{
float:right;
margin-top:-10px;
margin-right:0px;
}
#Panel1 legend
{
background-color:Green;
color:White;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<h1 style="text-align:left">Using The Google Weather API</h1><br />
<div style="text-align:left">
<b>Enter Location (Postal Code or City): </b><asp:TextBox ID="txtLocation" Text="10007" runat="server"></asp:TextBox>
<asp:Button ID="btnGo" runat="server" Text="GO" onclick="btnGo_Click" /><br /><br />
<asp:Panel ID="Panel1" runat="server">
<asp:Image ImageUrl="" runat="server" ID="icon" /> <br />
<p>
Forecast for <b><asp:Label ID="lblLocation" runat="server"></asp:Label></b><br />
Current Condition: <b><asp:Label ID="currCondition" runat="server" Text=""></asp:Label></b><br />
<b><asp:Label ID="temp_f" runat="server" CssClass = "temp" Text=""></asp:Label></b>
<b><asp:Label ID="temp_c" runat="server" Text=""></asp:Label></b><br />
<b><asp:Label ID="humidity" runat="server" Text=""></asp:Label></b><br />
<b><asp:Label ID="wind_condition" runat="server" Text=""></asp:Label></b><br />
Forecast Date: <b><asp:Label ID="lblForecastDate" runat="server" Text=""></asp:Label></b><br />
</p>
</asp:Panel><br />
<div>
<b><asp:Label id="wdcError" runat="server" Visible="false" CssClass="weatherError" ></asp:Label></b>
</div>
</div>
</form>
<br/>
</body>
</html>
.cs文件
protected void btnGo_Click(object sender, EventArgs e)
{
// retrieve weather for the zip code entered by the user
getWeatherData();
}
public void getWeatherData()
{
wdcError.Visible = false;
// check for empty zip code field
if (txtLocation.Text.Length == 0)
{
wdcError.Visible = true;
wdcError.Text = "Please enter a location!";
return;
}
// load xml result from Google weather
XDocument xd = XDocument.Load("http://www.google.com/ig/api?weather=" + txtLocation.Text);
// used to determine if a node is missing
int cnt = 0;
cnt = xd.Descendants("forecast_information").Count();
// determine if forecast information was returned for the location entered by user
if (cnt == 0)
{
wdcError.Visible = true;
wdcError.Text = "Forecast Information NOT available for the location";
return;
}
// navigate to the Current Conditions node
var current_conditions = from currentCond in xd.Root.Descendants("current_conditions")
select currentCond;
// navigate to the Forecast Information node
var forcastInfo = from forecastinfo in xd.Root.Descendants("forecast_information")
select forecastinfo;
Panel1.GroupingText = "Today's Weather";
// retrieve city and forecast date information
foreach (var item in forcastInfo)
{
lblLocation.Text = item.Element("city").Attribute("data").Value;
lblForecastDate.Text = item.Element("forecast_date").Attribute("data").Value;
}
// retrieve current weather conditions information
foreach (var item in current_conditions)
{
currCondition.Text = item.Element("condition").Attribute("data").Value;
temp_f.Text = item.Element("temp_f").Attribute("data").Value + "°" + "F";
temp_c.Text = " (" + item.Element("temp_c").Attribute("data").Value + "°" + "C" + ")";
humidity.Text = item.Element("humidity").Attribute("data").Value;
icon.ImageUrl = "http://www.google.com" + item.Element("icon").Attribute("data").Value;
wind_condition.Text = item.Element("wind_condition").Attribute("data").Value;
}
}
如果你最喜欢的天气应用程序在上周左右突然停止工作,那么尝试刷新或重新安装它是没有意义的。谷歌已经关闭了天气API,但没有任何消息,依赖它为天气相关应用程序供电的开发人员陷入困境。我们剩下的是一堆坏掉的应用程序,它们可能会被修复,也可能不会被修复。
它不见了,我的朋友试试别的
你可以从这里得到
http://www.yr.no/
http://www.weathercase.net/
MSN天气API条件列表?
感谢
这是因为http://www.google.com/ig/api?weather=" + txtLocation.Text
没有返回xml文件。该链接只是将您重定向到谷歌页面。
谷歌在之前的某个时候关闭了天气api。http://thenextweb.com/google/2012/08/28/did-google-just-quietly-kill-private-weather-api/
微软有一个天气API非常类似于谷歌的
查看此示例
http://weather.service.msn.com/data.aspx?weadegreetype=F&文化=en-US&weasearchstr=伊利诺伊州芝加哥
或者您可以使用Wunderground api