论坛首页 编程语言技术论坛

ruby+flex实现天气预报

浏览 8912 次
该帖已经被评为良好帖
作者 正文
   发表时间:2008-07-05  

      研究一段时间flex后发现,actionscript3和ruby一样具有很强的动态性,可以构建十分强大的客户端,但目前对于一些主要基于文本的系统来说有点杀鸡用牛刀的感觉,可是未来的客户端正在朝着多媒体化的方向发展,因为这样会带来更高的用户体验。感觉flex在客户端上有着很好的表现,而ruby在后台业务处理上非常的灵巧,随着两者不断的成熟与发展,有理由相信两者的结合一定会在企业级AIR应用上开辟一片新天地。

      下面是一个flex结合ruby on rails订阅天气预报的例子,实现起来比较简洁自然。

ruby端:

class WeathersController < ApplicationController
  require 'rss/1.0'          
  require 'rss/2.0'          
  require 'open-uri' 
  def show
    feed= "http://www.raychou.com/weather/rss.php?id=#{params[:code]}"        
    content = ""          
    open(feed) do |s|        
      content = s.read        
    end        
    @rss = RSS::Parser.parse(content, false)     
    render :xml => @rss.channel  #无需解析直接发送
  end
end

 

 flex端:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="624" height="426">
     <mx:Script>
     	<![CDATA[
     		import mx.events.CloseEvent;
     		import mx.rpc.events.ResultEvent;
     		[Bindable]
            private var cityName: Array = [ {label:"北京", data:"54511"},
                                            {label:"南京", data:"58238"},
                                            {label:"上海", data:"58367"}
                                          ];
            [Bindable]
            private var selectedItem:Object;
           //处理请求无需解析结果数据,直接用就可以了
        private function resultWeather(event:ResultEvent):void{
     	      item1.text = event.result.channel.item[0].title.toString();
     	      item2.text = event.result.channel.item[1].title.toString();
                     item3.text = event.result.channel.item[2].title.toString();
     		}
     	]]>
     </mx:Script>
     <!--向后台发送http请求-->
    <mx:HTTPService result="resultWeather(event);" id="getWeather" url="http://localhost:3000/weathers/show" useProxy="false">
	    <mx:request>
	        <code>{selectedItem.data}</code>
	    </mx:request>
	</mx:HTTPService>
	<mx:Panel x="171" y="54" width="418" height="333" layout="absolute" title="天气预报查询" fontWeight="bold" fontSize="13">
		<mx:ComboBox x="120" y="28" id="cmbCityName" dataProvider="{cityName}" close="selectedItem=ComboBox(event.target).selectedItem;
		getWeather.send();" width="164" fontSize="12"/>
		<mx:Label x="10" y="81" id="item1" text="" width="367" fontSize="14" color="#0A6464"/>
		<mx:Label x="10" y="126" id="item2" text="" width="367" fontSize="14"  color="#0A6464"/>
		<mx:Label x="10" y="171" id="item3" width="367" fontSize="14" color="#0A6464"/>
		<mx:Label x="37" y="30" text="请选者城市"/>
	</mx:Panel>
</mx:Application>

 

运行结果:

   发表时间:2008-08-07  
flex和rails结合可以考虑使用AMF,不知道是WebORB好还是RubyAMF好用,据说WebORB的bug要少一点
0 请登录后投票
   发表时间:2008-08-28  
很想了解多一点这方面的内容,不过感觉学习的资料很少。
0 请登录后投票
   发表时间:2009-02-18  
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="http.send();">
	<mx:HTTPService id="http" method="GET" url="http://rss.news.sohu.com/rss/guonei.xml" resultFormat="object" useProxy="false" fault="mx.controls.Alert.show(event.fault.faultString);">
	</mx:HTTPService>
	<mx:Panel left="10" top="10" right="10" bottom="10" layout="absolute" title="BlogReader({http.lastResult.rss.channel.title})" fontSize="14">
	<mx:DataGrid id="doPost" left="20" right="20" top="20" bottom="343" dataProvider="{http.lastResult.rss.channel.item}" change="btn.visible=true" fontSize="14">
		<mx:columns>
			<mx:DataGridColumn headerText="标题" dataField="title" showDataTips="true"/>
			<mx:DataGridColumn headerText="日期" dataField="pubDate"/>
		</mx:columns>
	</mx:DataGrid>
	<mx:TextArea left="20" right="20" height="300" bottom="46" text="{doPost.selectedItem.description}" fontSize="14"/>
	<mx:LinkButton id="btn" right="20" bottom="20" label="详细内容" visible="false" width="109" click="navigateToURL(new URLRequest(doPost.selectedItem.link));"/>
	</mx:Panel>
</mx:Application>

 上面的代码不论是在什么情况下都可以正常显示的,可我迷惑的是下面的代码只是改成你的天气预报地址却不行了,在我的桌面上报“安全沙箱”异常,请问这是什么原因呢?为什么上面的没有安全问题呢?

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="http.send();">
	<mx:HTTPService id="http" method="GET" url="http://weather.raychou.com/?/detail/54511/rss" resultFormat="object" useProxy="false" fault="mx.controls.Alert.show(event.fault.faultString);">
	</mx:HTTPService>
	<mx:Panel left="10" top="10" right="10" bottom="10" layout="absolute" title="BlogReader({http.lastResult.rss.channel.title})" fontSize="14">
	<mx:DataGrid id="doPost" left="20" right="20" top="20" bottom="343" dataProvider="{http.lastResult.rss.channel.item}" change="btn.visible=true" fontSize="14">
		<mx:columns>
			<mx:DataGridColumn headerText="标题" dataField="title" showDataTips="true"/>
			<mx:DataGridColumn headerText="日期" dataField="pubDate"/>
		</mx:columns>
	</mx:DataGrid>
	<mx:TextArea left="20" right="20" height="300" bottom="46" text="{http.lastResult.html.body}" fontSize="14"/>
	<mx:LinkButton id="btn" right="20" bottom="20" label="详细内容" visible="false" width="109" click="navigateToURL(new URLRequest(doPost.selectedItem.link));"/>
	</mx:Panel>
</mx:Application>

    所以测试这个,是因为我在我的服务器上写的xml也出现同样问题,不能在桌面上访问。知道原因的大侠们请多多赐教呀。。。

   先谢谢各位了!

0 请登录后投票
   发表时间:2009-02-18  
发现一个比较奇怪的现象,google reader上看到的有一条1970年1月1日的信息,不知道这又是什么原因?楼主设置了什么呢,还是访问方的原因呢?
0 请登录后投票
   发表时间:2009-02-18  
貌似用绑定的方式更加简单,代码也更少。
0 请登录后投票
   发表时间:2009-02-19  
第一次看flex的脚本,看来是一整个xml啊。
0 请登录后投票
   发表时间:2009-02-20  
呵呵,现在flex与ruby互相通信的方式有几种?
和java一样多么?
还是比较讨厌写过的类要在flex里再写一遍……
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics