Web Service学习笔记:利用YAHOO公开API做天气预报Web服务

难道是因为在这个地方,我放了个本人在我的小站的链接地址,就违反了首页的规则,这绝对的原创,我星星苦苦写的,虽说没有很高的技术含量,但也是有点的,在放一次,如果被撤下来,希望能给我个合理的解释。在此,谢谢园子里了的管理人员。

学了一段时间的Web服务,今天做了一个Web服务,利用YAHOO的公开天气API做自己的Web服务,主要是想练练手。现在把过程和心得分享给大家。

文章在我小站的地址:Web Service学习笔记:利用YAHOO公开API做天气预报Web服务


 

求教:这个Web服务还有个不完善的地方,Web服务的CityNameToCityNum方法,这个最重要,他是把省会和直辖市的名字转换为编号,因为YAHOO传的参数不是城市名字的区号,全是自己的,而我又想不到更好的获得YAHOO城市对应的编号的方法,所以就创建了HASHTABLE存储了中国的各个省会城市和直辖市,希望有高手提出更好的方法,能不用这样,直接找YAHOO获取编号,提取更多的城市,而不用把所有的中国所有的城市全写在HASHTABLE里。


Web服务地址:http://www.h2bbs.com/Weather/Weather.asmx

 

原理:

 在Yahoo的Developer Network

http://developer.yahoo.com/weather/

详细地介绍了Yahoo天气预报的API调用方法,这里用C#来实现,本文主要是利用它的API做Web服务,其它的应用由网友们自由发挥

首先了解Yahoo Weather Api的RSS Response格式(这是下午我查我家银川天气时返回的RSS):

 

<? xml version = " 1.0 "  encoding = " UTF-8 "  standalone = " yes "   ?>
< rss version = " 2.0 "  xmlns:yweather = " http://xml.weather.yahoo.com/ns/rss/1.0 "  xmlns:geo = " http://www.w3.org/2003/01/geo/wgs84_pos# " >
  
< channel >
    
< title > Yahoo !  Weather  -  Yinchuan, CH </ title >
    
< link > http: // us.rd.yahoo.com/dailynews/rss/weather/Yinchuan__CH/*[url] http://weather.yahoo.com/forecast/CHXX0259_f.html [/url]</link>
     < description > Yahoo !  Weather  for  Yinchuan, CH </ description >
    
< language > en - us </ language >
    
< lastBuildDate > Tue,  14  Oct  2008   11 : 00  am CST </ lastBuildDate >
    
< ttl > 60 </ ttl >
    
< yweather:location city = " Yinchuan "  region = ""   country = " CH " />
    
< yweather:units temperature = " F "  distance = " mi "  pressure = " in "  speed = " mph " />
    
< yweather:wind chill = " 56 "   direction = " 360 "   speed = " 4 "   />
    
< yweather:atmosphere humidity = " 56 "   visibility = " 999 "   pressure = ""   rising = " 0 "   />
    
< yweather:astronomy sunrise = " 7:03 am "   sunset = " 6:19 pm " />
    
< image >
      
< title > Yahoo !  Weather </ title >
      
< width > 142 </ width >
      
< height > 18 </ height >
      
< link > http: // weather.yahoo.com</link>
       < url > http: // l.yimg.com/us.yimg.com/i/us/nws/th/main_142b.gif</url>
     </ image >
    
< item >
      
< title > Conditions  for  Yinchuan, CH at  11 : 00  am CST </ title >
      
< geo:lat > 38.48 </ geo:lat >
      
< geo: long > 106.22 </ geo: long >
      
< link > http: // us.rd.yahoo.com/dailynews/rss/weather/Yinchuan__CH/*[url] http://weather.yahoo.com/forecast/CHXX0259_f.html [/url]</link>
       < pubDate > Tue,  14  Oct  2008   11 : 00  am CST </ pubDate >
      
< yweather:condition  text = " Mostly Cloudy "   code = " 28 "   temp = " 56 "   date = " Tue, 14 Oct 2008 11:00 am CST "   />
      
< description >
        
<! [CDATA[
< img src = "
[img]http: // l.yimg.com/us.yimg.com/i/us/we/52/28.gif[/img]"/><br />
< b > Current Conditions: </ b >< br  />
Mostly Cloudy, 
56  F < BR  />
< BR  />< b > Forecast: </ b >< BR  />
Tue 
-  Mostly Cloudy. High:  68  Low:  47 < br  />
Wed 
-  Partly Cloudy. High:  70  Low:  44 < br  />
< br  />
< a href = " Full'>http://us.rd.yahoo.com/dailynews/rss/weather/Yinchuan__CH/*http://weather.yahoo.com/forecast/CHXX0259_f.html " > Full Forecast at Yahoo !  Weather </ a >< BR />
(provided by The Weather Channel)
< br />
]]
>
      
</ description >
      
< yweather:forecast day = " Tue "  date = " 14 Oct 2008 "  low = " 47 "  high = " 68 "  text = " Mostly Cloudy "  code = " 28 "   />
      
< yweather:forecast day = " Wed "  date = " 15 Oct 2008 "  low = " 44 "  high = " 70 "  text = " Partly Cloudy "  code = " 30 "   />
      
< guid isPermaLink = " false " > CHXX0259_2008_10_14_11_00_CST </ guid >
    
</ item >
  
</ channel >
</ rss >
<!--  api5.weather.sp1.yahoo.com compressed / chunked Mon Oct  13   22 : 30 : 39  PDT  2008   -->

 

 

 

其中最重要的是后面的几行,查询当天和第二天的天气情况,我们要获取的天气信息就在里面,代码如下:

 

 

      < yweather:forecast day = " Tue "  date = " 14 Oct 2008 "  low = " 47 "  high = " 68 "  text = " Mostly Cloudy "  code = " 28 "   />
      
< yweather:forecast day = " Wed "  date = " 15 Oct 2008 "  low = " 44 "  high = " 70 "  text = " Partly Cloudy "  code = " 30 "   />

 

我们所需要用到的Node是/rss/channel/item/yweather: forecast;这个节点里的东西是我们需要的。


知道了这些,我们接下来要做的就是在VS中建立WEB服务,并添加下面的代码:

 

 

 

namespace  Weather
ExpandedBlockStart.gifContractedBlock.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// Weather 的摘要说明
    
/// </summary>

    [WebService(Name = "WeatherWebService", Namespace = "http://www.h2bbs.com/Weather/Weather.asmx", Description = "利用YAHOO公开的API接口,在Web服务实现公开的天气预报调用服务。")]
    [WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(
false)]
    
public class Weather : System.Web.Services.WebService
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        [WebMethod(Description 
= "根据城市的名称返回该城市的天气预报情况,只能查询省会和直辖市的天气情况。")]
        
public DataSet GetWeather(string strCityName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
string strXml = "http://xml.weather.yahoo.com/forecastrss?p=CHXX" + CityNameToCityNum(strCityName);

            XmlDocument Weather 
= new XmlDocument();
            Weather.Load(strXml);

            XmlNamespaceManager namespacemanager 
= new XmlNamespaceManager(Weather.NameTable);
            namespacemanager.AddNamespace(
"yweather""http://xml.weather.yahoo.com/ns/rss/1.0");
            XmlNodeList nodes 
= Weather.SelectNodes("/rss/channel/item/yweather:forecast", namespacemanager);

            
//XmlNodeList nodes = Weather.GetElementsByTagName("forecast", "http://xml.weather.yahoo.com/ns/rss/1.0");

            DataSet dstWeather 
= new DataSet();
            DataTable dtblWeather 
= new DataTable("Weather");
            dstWeather.Tables.Add(dtblWeather);

            dstWeather.Tables[
"Weather"].Columns.Add("Date"typeof(string));
            dstWeather.Tables[
"Weather"].Columns.Add("Week"typeof(string));
            dstWeather.Tables[
"Weather"].Columns.Add("Weather"typeof(string));
            dstWeather.Tables[
"Weather"].Columns.Add("Tlow"typeof(string));
            dstWeather.Tables[
"Weather"].Columns.Add("Thigh"typeof(string));

            
if (nodes.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
foreach (XmlNode node in nodes)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    DataRow drowWeather 
= dstWeather.Tables["Weather"].NewRow();

                    drowWeather[
"Date"= EmonthToCmonth(node.SelectSingleNode("@date").Value.ToString());
                    drowWeather[
"Week"= EweekToCweek(node.SelectSingleNode("@day").Value.ToString()) + "(" + node.SelectSingleNode("@day").Value.ToString() + ")";
                    drowWeather[
"Weather"= node.SelectSingleNode("@text").Value;
                    drowWeather[
"Tlow"= FToC(int.Parse(node.SelectSingleNode("@low").Value)) + "";
                    drowWeather[
"Thigh"= FToC(int.Parse(node.SelectSingleNode("@high").Value)) + "";

                    dstWeather.Tables[
"Weather"].Rows.Add(drowWeather);
                }


                
return dstWeather;
            }

            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                DataRow drowNone 
= dstWeather.Tables["Weather"].NewRow();
                drowNone[
"Date"= "None";
                drowNone[
"Week"= "None";
                drowNone[
"Weather"= "None";
                drowNone[
"Tlow"= "None";
                drowNone[
"Thigh"= "None";

                dstWeather.Tables[
"Weather"].Rows.Add(drowNone);
                
return dstWeather;
            }

        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 从华氏转换成摄氏
        
/// </summary>
        
/// <param name="f">华氏度</param>
        
/// <returns></returns>

        private string FToC(int f)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return Math.Round((f - 32/ 1.81).ToString();
        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 从星期英文缩写转汉字
        
/// </summary>
        
/// <param name="strEweek">星期的英文缩写</param>
        
/// <returns></returns>

        private string EweekToCweek(string strEweek)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
switch (strEweek)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
case "Mon":
                    
return "星期一";
                    
break;
                
case "Tue":
                    
return "星期二";
                    
break;
                
case "Wed":
                    
return "星期三";
                    
break;
                
case "Thu":
                    
return "星期四";
                    
break;
                
case "Fri":
                    
return "星期五";
                    
break;
                
case "Sat":
                    
return "星期六";
                    
break;
                
case "Sun":
                    
return "星期日";
                    
break;
                
default:
                    
return "传参错误";
                    
break;
            }

        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 从月英文缩写转汉字
        
/// </summary>
        
/// <param name="strReplace">需要替换的年月日</param>
        
/// <returns></returns>

        private string EmonthToCmonth(string strReplace)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return Convert.ToDateTime(strReplace).ToString("yyyy年MM月dd日");
        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 根据城市名称返回城市编号
        
/// </summary>
        
/// <param name="strCityName">城市名称</param>
        
/// <returns></returns>

        private string CityNameToCityNum(string strCityNameToNum)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//中国各个省会和直辖市对应的查询代码
            Hashtable htWeather = new Hashtable();
            htWeather.Add(
"北京""0008");
            htWeather.Add(
"天津""0133");
            htWeather.Add(
"杭州""0044");
            htWeather.Add(
"合肥""0448");
            htWeather.Add(
"上海""0116");
            htWeather.Add(
"福州""0031");
            htWeather.Add(
"重庆""0017");
            htWeather.Add(
"南昌""0097");
            htWeather.Add(
"香港""0049");
            htWeather.Add(
"济南""0064");
            htWeather.Add(
"澳门""0512");
            htWeather.Add(
"郑州""0165");
            htWeather.Add(
"呼和浩特""0249");
            htWeather.Add(
"乌鲁木齐""0135");
            htWeather.Add(
"长沙""0013");
            htWeather.Add(
"银川""0259");
            htWeather.Add(
"广州""0037");
            htWeather.Add(
"拉萨""0080");
            htWeather.Add(
"海口""0502");
            htWeather.Add(
"南宁""0100");
            htWeather.Add(
"成都""0016");
            htWeather.Add(
"石家庄""0122");
            htWeather.Add(
"贵阳""0039");
            htWeather.Add(
"太原""0129");
            htWeather.Add(
"昆明""0076");
            htWeather.Add(
"沈阳""0119");
            htWeather.Add(
"西安""0141");
            htWeather.Add(
"长春""0010");
            htWeather.Add(
"兰州""0079");
            htWeather.Add(
"西宁""0236");
            htWeather.Add(
"南京""0099");

            
object cityNum = htWeather[strCityNameToNum];

            
if (cityNum == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return "City not found";
            }

            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return cityNum.ToString();
            }

        }

    }

}

 

 

别忘了"using System.Xml;"哦!

 

Web服务的代码中有一个Web公开方法,四个私有方法:

(1)GetWeather方法是公共方法,提供Web调用。

(2)FToC方法,他主要是把RSS返回的华氏温度转换成摄氏温度,其实这一步可以不用的,当初没发现,URL中加点参数就返回的是摄氏温度。

(3)EweekToCweek方法,他主要是把英文的星期缩写变成中文。

(4)EmonthToCmonth方法,它主要是把英文的月份缩写变成中文,并重新排序。

(5)CityNameToCityNum方法,这个最重要,他是把省会和直辖市的名字转换为编号,因为YAHOO传的参数不是城市名字的区号,全是自己的,而我又想不到更好的获得YAHOO城市对应的编号的方法,所以就只能支持这么几个城市了,希望有高手提出更好的方法,能不用这样,直接找YAHOO获取编号。

查询个中国随便的省会,效果如下

 

  <? xml version = " 1.0 "  encoding = " utf-8 "   ?>  

-   < DataSet xmlns = " http://www.h2bbs.com/WebService/Weather.asmx " >


-   < xs:schema id = " NewDataSet "  xmlns = ""  xmlns:xs = " http://www.w3.org/2001/XMLSchema "  xmlns:msdata = " urn:schemas-microsoft-com:xml-msdata " >


-   < xs:element name = " NewDataSet "  msdata:IsDataSet = " true "  msdata:UseCurrentLocale = " true " >


-   < xs:complexType >


-   < xs:choice minOccurs = " 0 "  maxOccurs = " unbounded " >


-   < xs:element name = " Weather " >


-   < xs:complexType >


-   < xs:sequence >


  
< xs:element name = " Date "  type = " xs:string "  minOccurs = " 0 "   />  

  
< xs:element name = " Week "  type = " xs:string "  minOccurs = " 0 "   />  

  
< xs:element name = " Weather "  type = " xs:string "  minOccurs = " 0 "   />  

  
< xs:element name = " Tlow "  type = " xs:string "  minOccurs = " 0 "   />  

  
< xs:element name = " Thigh "  type = " xs:string "  minOccurs = " 0 "   />  
  
</ xs:sequence >
  
</ xs:complexType >
  
</ xs:element >
  
</ xs:choice >
  
</ xs:complexType >
  
</ xs:element >
  
</ xs:schema >

-   < diffgr:diffgram xmlns:msdata = " urn:schemas-microsoft-com:xml-msdata "  xmlns:diffgr = " urn:schemas-microsoft-com:xml-diffgram-v1 " >


-   < NewDataSet xmlns = "" >


-   < Weather diffgr:id = " Weather1 "  msdata:rowOrder = " 0 "  diffgr:hasChanges = " inserted " >


  
< Date > 2008年10月14日 </ Date >  

  
< Week > 星期二(Tue) </ Week >  

  
< Weather > Clear </ Weather >  

  
< Tlow > 16.1 </ Tlow >  

  
< Thigh > 26.7 </ Thigh >  
  
</ Weather >

-   < Weather diffgr:id = " Weather2 "  msdata:rowOrder = " 1 "  diffgr:hasChanges = " inserted " >


  
< Date > 2008年10月15日 </ Date >  

  
< Week > 星期三(Wed) </ Week >  

  
< Weather > Sunny </ Weather >  

  
< Tlow > 16.7 </ Tlow >  

  
< Thigh > 28.3 </ Thigh >  
  
</ Weather >
  
</ NewDataSet >
  
</ diffgr:diffgram >
  
</ DataSet >

 

 

写完了!最后希望能给大家带来收获!

转载于:https://www.cnblogs.com/VisualStudio/archive/2008/10/14/1311187.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,你需要准备好以下材料: - ESP8266开发板 - 飞线和面包板 - DHT11温湿度传感器(可选) - USB转串口模块 - 计算机或笔记本电脑 然后,按照以下步骤进行操作: 1. 注册和风天气开发者账号,获取自己的API Key。 2. 在Arduino IDE中安装ESP8266的开发环境,并将开发板设置为ESP8266。 3. 下载并安装ESP8266WiFi库和ArduinoJson库,用于连接WiFi和解析JSON数据。 4. 连接ESP8266开发板和DHT11传感器,根据需要使用面包板和飞线。 5. 在Arduino IDE中编写代码,实现以下功能: - 连接WiFi网络; - 读取DHT11传感器数据(可选); - 调用和风天气API获取天气预报数据; - 解析JSON数据,获取需要的天气信息; - 将天气信息显示在串口监视器中或者OLED显示屏上(可选)。 下面是一个简单的示例代码,你可以根据自己的需求进行修改: ```c++ #include <ESP8266WiFi.h> #include <ArduinoJson.h> const char* ssid = "your_SSID"; const char* password = "your_PASSWORD"; const char* host = "free-api.heweather.net"; const char* apiKey = "your_API_KEY"; void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("Connected to WiFi"); } void loop() { float temperature = readTemperature(); float humidity = readHumidity(); String url = "/s6/weather/now?location=your_LOCATION&key=" + String(apiKey); Serial.print("Connecting to "); Serial.println(host); WiFiClient client; if (!client.connect(host, 80)) { Serial.println("Connection failed"); return; } Serial.print("Requesting URL: "); Serial.println(url); client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); while (!client.available()) { delay(100); } String response = client.readString(); Serial.println(response); StaticJsonDocument<512> doc; deserializeJson(doc, response); JsonObject root = doc.as<JsonObject>(); JsonObject now = root["HeWeather6"][0]["now"]; String weather = now["cond_txt"].as<String>(); int temperature = now["tmp"].as<int>(); int humidity = now["hum"].as<int>(); Serial.print("Weather: "); Serial.println(weather); Serial.print("Temperature: "); Serial.print(temperature); Serial.println("°C"); Serial.print("Humidity: "); Serial.print(humidity); Serial.println("%"); delay(60000); } float readTemperature() { // code for reading temperature from DHT11 sensor } float readHumidity() { // code for reading humidity from DHT11 sensor } ``` 在代码中,你需要将`your_SSID`和`your_PASSWORD`替换为连接WiFi网络的SSID和密码,将`your_API_KEY`替换为你的和风天气API Key,将`your_LOCATION`替换为你要查询的城市名称或城市ID。同时,你还需要添加读取DHT11传感器数据的代码,或者使用其他类型的传感器替换它。 最后,上传代码到ESP8266开发板,打开串口监视器,就可以看到ESP8266调用和风天气API获取天气预报数据,并将天气信息显示在串口监视器中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值