yahoo OPEN api测试之weather feed

简介:
 昨天写了个 google feed api 应用 , 突然对 rss 蛮感兴趣的 , 天生比较好奇 , 以前都是做 windows 程序感觉进入互联网领域 , 象是走进另外一扇大门 , 更加有趣 , 更加丰富多彩 .先展示一下结果
调用feed api最主要要学习的知识是RSS读取的问题.
         .NET框架提供了System.Xml.Serialization.XmlSerializer类型,为将对象序列化为XML或将XML序列化为对象提供了很大支持。
         下面我们针对yahoo weather feed api,通过实例讲解一下如何读取RSS文件。
         首先通过yahoo weather feed api
         "http://xml.weather.yahoo.com/forecastrss?p=" & p & "&u=" & U
     可以得到如下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 - Hangzhou, CH</title>
                  <link>http://us.rd.yahoo.com/dailynews/rss/weather/Hangzhou__CH/*http://weather.yahoo.com/forecast/CHXX0044_f.html</link>
                   <description>Yahoo! Weather for Hangzhou, CH</description>
                   <language>en-us</language>
                   <lastBuildDate>Fri, 27 Feb 2009 12:30 pm CST</lastBuildDate>
                   <ttl>60</ttl>
                   <yweather:location city="Hangzhou" region="" country="CH" />
                   <yweather:units temperature="F" distance="mi" pressure="in" speed="mph" />
                   <yweather:wind chill="39" direction="20" speed="7" />
                   <yweather:atmosphere humidity="81" visibility="1.86" pressure="30.27" rising="0" />
                   <yweather:astronomy sunrise="6:28 am" sunset="5:57 pm" />
                   <image>
                            <title>Yahoo! Weather</title>
                            <width>142</width>
                            <height>18</height>
                            <link>http://weather.yahoo.com</link>
                            <url>http://l.yimg.com/a/i/us/nws/th/main_142b.gif</url>
                   </image>
                   <item>
                            <title>Conditions for Hangzhou, CH at 12:30 pm CST</title>
                            <geo:lat>30.23</geo:lat>
                            <geo:long>120.17</geo:long>
                            <link>http://us.rd.yahoo.com/dailynews/rss/weather/Hangzhou__CH/*http://weather.yahoo.com/forecast/CHXX0044_f.html</link>
                            <pubDate>Fri, 27 Feb 2009 12:30 pm CST</pubDate>
                            <yweather:condition text="Rain Shower" code="11" temp="43" date="Fri, 27 Feb 2009 12:30 pm CST" />
                            <description>
                                     <![CDATA[
                                     <img src="http://l.yimg.com/a/i/us/we/52/11.gif"/>
                                     <br />
                                     <b>Current Conditions:</b>
                                     <br />
                                     Rain Shower, 43 F<BR />
                                     <BR />
                                     <b>Forecast:</b>
                                     <BR />
                                     Fri - Rain. High: 42 Low: 39<br />
                                     Sat - Rain. High: 43 Low: 40<br />
                                     <br />
                                     <a href="Full'>http://us.rd.yahoo.com/dailynews/rss/weather/Hangzhou__CH/*http://weather.yahoo.com/forecast/CHXX0044_f.html">Full Forecast at Yahoo! Weather</a>
                                     <BR/>
                                     (provided by The Weather Channel)<br/>
                                     ]]>
                            </description>
                            <yweather:forecast day="Fri" date="27 Feb 2009" low="39" high="42" text="Rain" code="12" />
                            <yweather:forecast day="Sat" date="28 Feb 2009" low="40" high="43" text="Rain" code="12" />
                            <guid isPermaLink="false">CHXX0044_2009_02_27_12_30_CST</guid>
                   </item>
         </channel>
</rss>
<!-- api4.weather.sp1.yahoo.com uncompressed/chunked Thu Feb 26 21:00:04 PST 2009 -->
 
 
 
如上文所说.NET框架提供了System.Xml.Serialization.XmlSerializer类型,现在我们要做的就是如何利用xmlSerializer将XML序列化为对象。
         对于上面的XML文件为了便于对象序列化我们会做如下替换
        
    str = str.Replace("yweather:", "")
        str = str.Replace("geo:", "")
        'Dim str1 As String = """  : "
 
        Dim str1 = "isPermaLink=""false"""
        str = str.Replace(str1, "")
        str1 = " 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#"""
        str = str.Replace(str1, "")
 
 
接着我们需要按照xml的格式,将所有的节点定义为对象,并将节点的属性定义为对象的属性。
         如该RSS中需要先定义rss对象
        
Imports Microsoft.VisualBasic
Imports System.Xml.Serialization
Partial Class YahooWeatherForecast
 
    <XmlRoot("rss")> _
       Public Class rssClass
        Private _channel As channelClass
 
        <XmlElement()> _
        Public Property channel() As channelClass
            Get
                Return _channel
            End Get
            Set(ByVal value As channelClass)
                _channel = value
            End Set
        End Property
    End Class
End Class
 
在该对象中包含chanelClass,保持和xml的文件相同的结构
接下来我们需要定义chanelClass,代码如下:
Imports Microsoft.VisualBasic
Imports System.Xml.Serialization
Partial Class YahooWeatherForecast
    Partial Class rssClass
        Partial Class channelClass
            Private _title As String
            ''' <summary>
            '''  The title of the feed, which includes the location city. For example "Yahoo! Weather - Sunnyvale, CA"
            ''' </summary>
            <XmlElement()> _
            Public Property title() As String
                Get
                    Return _title
                End Get
                Set(ByVal value As String)
                    _title = value
                End Set
            End Property
 
 
            Private _link As String
            ''' <summary>
            '''  The URL for the Yahoo! Weather page of the forecast for this location. For example http://us.rd.yahoo.com/dailynews/rss/weather/ Sunnyvale__CA/ *http://xml.weather.yahoo.com/ forecast/94089_f.html
            ''' </summary>
            <XmlElement()> _
            Public Property link() As String
                Get
                    Return _link
                End Get
                Set(ByVal value As String)
                    _link = value
                End Set
            End Property
 
            Private _language As String
            ''' <summary>
            '''  The language of the weather forecast, for example, en-us for US English.
            ''' </summary>
            <XmlElement()> _
            Public Property language() As String
                Get
                    Return _language
                End Get
                Set(ByVal value As String)
                    _language = value
                End Set
            End Property
 
            Private _description As String
            ''' <summary>
            '''  The overall description of the feed including the location, for example "Yahoo! Weather for Sunnyvale, CA"
            ''' </summary>
            <XmlElement()> _
            Public Property description() As String
                Get
                    Return _description
                End Get
                Set(ByVal value As String)
                    _description = value
                End Set
            End Property
 
            Private _lastBuildDate As String
            ''' <summary>
            '''  The last time the feed was updated. The format is in the date format defined by RFC822 Section 5, for example Mon, 256 Sep 17:25:18 -0700.
            ''' </summary>
            <XmlElement()> _
            Public Property lastBuildDate() As String
                Get
                    Return _lastBuildDate
                End Get
                Set(ByVal value As String)
                    _lastBuildDate = value
                End Set
            End Property
 
            Private _ttl As String
            ''' <summary>
            '''  Time to Live; how long in minutes this feed should be cached.
            ''' </summary>
            <XmlElement()> _
            Public Property ttl() As String
                Get
                    Return _ttl
                End Get
                Set(ByVal value As String)
                    _ttl = value
                End Set
            End Property
 
            Private _location As locationClass
            ''' <summary>
            '''  The location of this forecast.
            ''' </summary>
            <XmlElement()> _
            Public Property location() As locationClass
                Get
                    Return _location
                End Get
                Set(ByVal value As locationClass)
                    _location = value
                End Set
            End Property
 
            Private _units As unitsClass
            ''' <summary>
            '''  Units for various aspects of the forecast.
            ''' </summary>
            <XmlElement()> _
            Public Property units() As unitsClass
                Get
                    Return _units
                End Get
                Set(ByVal value As unitsClass)
                    _units = value
                End Set
            End Property
 
            Private _wind As windClass
            ''' <summary>
            '''  Forecast information about wind.
            ''' </summary>
            <XmlElement()> _
            Public Property wind() As windClass
                Get
                    Return _wind
                End Get
                Set(ByVal value As windClass)
                    _wind = value
                End Set
            End Property
 
            Private _atmosphere As atmosphereClass
            ''' <summary>
            '''  Forecast information about current atmospheric pressure, humidity, and visibility.
            ''' </summary>
            <XmlElement()> _
            Public Property atmosphere() As atmosphereClass
                Get
                    Return _atmosphere
                End Get
                Set(ByVal value As atmosphereClass)
                    _atmosphere = value
                End Set
            End Property
 
            Private _astronomy As astronomyClass
            ''' <summary>
            '''  Forecast information about current astronomical conditions.
            ''' </summary>
            <XmlElement()> _
            Public Property astronomy() As astronomyClass
                Get
                    Return _astronomy
                End Get
                Set(ByVal value As astronomyClass)
                    _astronomy = value
                End Set
            End Property
            Private _image As imageClass
            ''' <summary>
            '''  The image used to identify this feed
            ''' </summary>
            <XmlElement()> _
            Public Property image() As imageClass
                Get
                    Return _image
                End Get
                Set(ByVal value As imageClass)
                    _image = value
                End Set
            End Property
            Private _item As itemClass
            ''' <summary>
            '''  The local weather conditions and forecast for a specific location.
            ''' </summary>
            <XmlElement()> _
            Public Property item() As itemClass
                Get
                    Return _item
                End Get
                Set(ByVal value As itemClass)
                    _item = value
                End Set
            End Property
        End Class
    End Class
End Class
 
 
在chanel中声明了他包含哪些子节点,同样我们也需要为这些子节点申明对象。
Imports Microsoft.VisualBasic
Imports System.Xml.Serialization
Partial Class YahooWeatherForecast
    Partial Class rssClass
        Partial Class channelClass
            Public Class atmosphereClass
                Private _humidity As Integer
                ''' <summary>
                '''  humidity, in percent
                ''' </summary>
                <XmlAttribute()> _
                Public Property humidity() As Integer
                    Get
                        Return _humidity
                    End Get
                    Set(ByVal value As Integer)
                        _humidity = value
                    End Set
                End Property
                Private _visibility As Double
                ''' <summary>
                '''   in the units specified by the distance attribute of the units element (mi or km). Note that the visibility is specified as the actual value * 100. For example, a visibility of 16.5 miles will be specified as 1650. A visibility of 14 kilometers will appear as 1400.
                ''' </summary>
                <XmlAttribute()> _
                Public Property visibility() As Double
                    Get
                        Return _visibility
                    End Get
                    Set(ByVal value As Double)
                        _visibility = value
                    End Set
                End Property
 
                Private _pressure As Double
                ''' <summary>
                '''  barometric pressure, in the units specified by the pressure attribute of the units element (in or mb).
                ''' </summary>
                <XmlAttribute()> _
                Public Property pressure() As Double
                    Get
                        Return _pressure
                    End Get
                    Set(ByVal value As Double)
                        _pressure = value
                    End Set
                End Property
 
 
                Private _rising As Integer
                ''' <summary>
                '''  state of the barometric pressure,   steady = 0, rising = 1, falling = 2
                ''' </summary>
                <XmlAttribute()> _
                Public Property rising() As Integer
                    Get
                        Return _rising
                    End Get
                    Set(ByVal value As Integer)
                        _rising = value
                    End Set
                End Property
            End Class
        End Class
    End Class
End Class
 
在申明完对象之后,我们就可以将xml文件转换为相应的对象。
代码如下:
      Dim doc As New System.Xml.XmlDocument()
        Dim str As String = "http://xml.weather.yahoo.com/forecastrss?p=" & p & "&u=" & U
        Dim c As rssClass
        'Load data  
        doc.Load(str)
        str = doc.OuterXml
 
 
        'XmlSerializer could not Serialize XML with this Elements so i just replace them with Null String!
        str = str.Replace("yweather:", "")
        str = str.Replace("geo:", "")
        'Dim str1 As String = """  : "
 
        Dim str1 = "isPermaLink=""false"""
        str = str.Replace(str1, "")
        str1 = " 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#"""
        str = str.Replace(str1, "")
        Dim memoryStream As New IO.MemoryStream()
        Dim d As New IO.StringReader(str)
 
        Dim xs As New XmlSerializer(GetType(rssClass))
 
        c = CType(xs.Deserialize(d), rssClass)
 
        rss = c
 
 
最后用户可以根据自己的需要将对象中的属性取出,使用于相关的应用中。类似如下代码
       Dim t As New YahooWeatherLib.YahooWeatherForecast("CHXX0044", "f")
        Label1.Text = t.rss.channel.item.forecast.high
        Label2.Text = t.rss.channel.item.forecast.low
        Label5.Text = t.rss.channel.item.title


本文转自elbertchen 51CTO博客,原文链接:http://blog.51cto.com/linkyou/282659,如需转载请自行联系原作者
相关文章
|
3月前
|
人工智能 数据可视化 测试技术
Postman 性能测试教程:快速上手 API 压测
本文介绍API上线后因高频调用导致服务器告警,通过Postman与Apifox进行压力测试排查性能瓶颈。对比两款工具在批量请求、断言验证、可视化报告等方面的优劣,探讨API性能优化策略及行业未来发展方向。
Postman 性能测试教程:快速上手 API 压测
|
4月前
|
XML 安全 测试技术
【干货满满】分享什么是API接口测试
API接口测试是验证应用程序编程接口功能、性能、安全性及兼容性的关键环节,通过模拟请求并验证响应结果,确保接口能正确处理各种输入和场景。测试内容涵盖功能验证、性能评估、安全防护、兼容性验证及系统可靠性。相比UI测试,API测试无需界面依赖,支持数据驱动与自动化,适用于持续集成流程。常见接口类型包括RESTful、SOAP和GraphQL API,广泛应用于电商、金融及社交平台,保障系统间数据交互的安全与高效。
|
5月前
|
人工智能 监控 安全
API安全测试工具:数字经济的免疫防线
API安全面临漏洞盲区、配置错误与合规碎片三大挑战,传统手段难抵新型风险。破局需构建智能漏洞探针、配置审计中枢与合规映射引擎三位一体防御矩阵。Burp Suite、Noname Security、Traceable AI与板栗看板等工具助力企业实现自动化检测、精准响应与高效合规,打造API安全免疫体系。
|
5月前
|
JSON JavaScript 测试技术
用Postman玩转电商API:一键测试+自动化请求教程
Postman 是电商 API 测试的高效工具,涵盖基础配置、自动化测试、环境管理与请求自动化,助你快速提升开发效率。
|
3月前
|
人工智能 数据可视化 测试技术
AI 时代 API 自动化测试实战:Postman 断言的核心技巧与实战应用
AI 时代 API 自动化测试实战:Postman 断言的核心技巧与实战应用
471 11
|
8月前
|
监控 测试技术 数据库连接
RunnerGo API 性能测试实战:从问题到解决的全链路剖析
API性能测试是保障软件系统稳定性与用户体验的关键环节。本文详细探讨了使用RunnerGo全栈测试平台进行API性能测试的全流程,涵盖测试计划创建、场景设计、执行分析及优化改进。通过电商平台促销活动的实际案例,展示了如何设置测试目标、选择压测模式并分析结果。针对发现的性能瓶颈,提出了代码优化、数据库调优、服务器资源配置和缓存策略等解决方案。最终,系统性能显著提升,满足高并发需求。持续关注与优化API性能,对系统稳定运行至关重要。
|
4月前
|
监控 安全 测试技术
API测试工具评测:Apipost与Apifox的优劣深度解读
本文对比了Apipost与Apifox在API设计、数据建模、代码生成、测试能力、协作权限、性能监控、插件生态、文档管理及安全合规等方面的差异。Apifox在专业性、自动化、扩展性及团队协作上表现更优,尤其适合中大型项目与复杂管理需求,而Apipost功能较基础,适用于轻量级使用场景。
|
4月前
|
JSON 安全 测试技术
什么是API接口测试?这可能是全网最全的教程了!
API 是应用程序间的“中间人”,用于实现通信和数据交换。随着微服务架构的普及,API 数量激增,其质量对系统稳定性至关重要。API 测试可验证功能、性能与安全性,帮助开发者在部署前发现并修复问题,提升系统可靠性。测试内容包括请求方法、URL、请求头、请求体、响应状态码与响应数据等。常用工具如 Postman、AREX 可辅助测试,确保 API 在不同场景下的正确性与稳定性。
|
5月前
|
人工智能 自然语言处理 测试技术
AI时代,Apipost和Apifox如何利用AI技术赋能API研发测试管理所需?
在数字化转型加速背景下,API成为企业互联互通的关键。Apipost与Apifox作为主流工具,在AI赋能方面差异显著。Apipost通过智能参数命名、接口设计自动化、测试用例生成、断言自动化等功能大幅提升研发效率和质量,尤其适合中大型企业及复杂业务场景。相比之下,Apifox功能依赖手动操作较多,适用性更偏向初创或小型项目。随着AI技术发展,Apipost展现出更强的智能化与前瞻性优势,为企业提供高效、稳定的API管理解决方案,助力其在竞争激烈的市场中实现创新突破。
149 0
|
8月前
|
监控 测试技术 数据库连接
利用 RunnerGo 深度探索 API 性能测试:从理论到实践
API性能测试是保障应用稳定性和用户体验的关键环节。本文详细探讨了如何使用RunnerGo全栈测试平台进行高效API性能测试,涵盖测试计划创建、场景设计、参数配置到执行与分析全过程。通过电商平台促销活动案例,展示了高并发下的测试策略与优化措施,如代码与数据库查询优化、数据库连接池扩容、服务器资源配置调整及缓存策略实施等。最终显著提升系统性能,满足高并发需求。API性能测试需持续关注与优化,以适应业务发展和用户需求变化。
273 33