2014-04-20 45 views
0

嘿大家我是新來使用谷歌API獲取天氣更新。它的問題給出了一個錯誤.. 我設置的doctype,但問題沒有解決..請幫助我在此先感謝...錯誤''文檔類型'是一個意外的標記。預期的標記是'DOCTYPE'。第1行,第3位。

.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 + "&deg;" + "F"; 
       temp_c.Text = " (" + item.Element("temp_c").Attribute("data").Value + "&deg;" + "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; 
      } 
     } 

回答

1

如果你喜歡的天氣應用程序突然停在上週工作左右,嘗試刷新或重新安裝是沒有意義的。 Google一言不發地關閉了它的天氣API,並擱置了依靠它開發天氣相關應用程序的開發人員。我們剩下的是一堆破碎的應用程序,可能會修復或不修復。

它不見了我的朋友試試別的

那麼你可以從這裏

http://www.yr.no/

http://www.weathercase.net/

MSN weather API list of conditions?

由於得到

1

這是因爲你的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&culture=en-US&weasearchstr=Chicago,IL

或者你可以使用Wunderground api

+0

現在什麼SH我應該嗎,,,? –

+0

gud luck !!! ..... :) –

相關問題