ruby+flex實現天氣預報

來源:互聯網
上載者:User

 研究一段時間flex後發現,actionscript3和ruby一樣具有很強的動態性,可以構建十分強大的用戶端,但目前對於一些主要基於文本的系統來說有點殺雞用牛刀的感覺,可是未來的用戶端正在朝著多媒體化的方向發展,因為這樣會帶來更高的使用者體驗。感覺flex在用戶端上有著很好的表現,而ruby在後台業務處理上非常的靈巧,隨著兩者不斷的成熟與發展,有理由相信兩者的結合一定會在企業級AIR應用上開闢一片新天地。

      下面是一個flex結合ruby on rails訂閱天氣預報的例子,實現起來比較簡潔自然。

ruby端:

Ruby代碼
  1. class WeathersController < ApplicationController   
  2.   require 'rss/1.0'             
  3.   require 'rss/2.0'             
  4.   require 'open-uri'    
  5.   def show   
  6.     feed= "http://www.raychou.com/weather/rss.php?id=#{params[:code]}"           
  7.     content = ""             
  8.     open(feed) do |s|           
  9.       content = s.read           
  10.     end           
  11.     @rss = RSS::Parser.parse(content, false)        
  12.     render :xml => @rss.channel  <SPAN style="COLOR: #ff0000">#無需解析直接發送</SPAN>   
  13.   end  
  14. end  
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  #無需解析直接發送  endend

 

 flex端:

Java代碼
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="624" height="426">   
  3.      <mx:Script>   
  4.         <![CDATA[   
  5.             import mx.events.CloseEvent;   
  6.             import mx.rpc.events.ResultEvent;   
  7.             [Bindable]   
  8.             private var cityName: Array = [ {label:"北京", data:"54511"},   
  9.                                             {label:"南京", data:"58238"},   
  10.                                             {label:"上海", data:"58367"}   
  11.                                           ];   
  12.             [Bindable]   
  13.             private var selectedItem:Object;   
  14.            <SPAN style="COLOR: #ff0000">//<SPAN>處理請求</SPAN>,<SPAN>無需解析結果資料,直接用就可以了</SPAN></SPAN>   
  15.         private function resultWeather(event:ResultEvent):void{   
  16.               item1.text = event.result.channel.item[0].title.toString();   
  17.               item2.text = event.result.channel.item[1].title.toString();   
  18.                      item3.text = event.result.channel.item[2].title.toString();   
  19.             }   
  20.         ]]>   
  21.      </mx:Script>   
  22.      <!--向後台發送http請求-->   
  23.     <mx:HTTPService result="resultWeather(event);" id="getWeather" url="http://localhost:3000/weathers/show" useProxy="false">   
  24.         <mx:request>   
  25.             <code>{selectedItem.data}</code>   
  26.         </mx:request>   
  27.     </mx:HTTPService>   
  28.     <mx:Panel x="171" y="54" width="418" height="333" layout="absolute" title="天氣預報查詢" fontWeight="bold" fontSize="13">   
  29.         <mx:ComboBox x="120" y="28" id="cmbCityName" dataProvider="{cityName}" close="selectedItem=ComboBox(event.target).selectedItem;   
  30.         getWeather.send();" width="164" fontSize="12"/>   
  31.         <mx:Label x="10" y="81" id="item1" text="" width="367" fontSize="14" color="#0A6464"/>   
  32.         <mx:Label x="10" y="126" id="item2" text="" width="367" fontSize="14"  color="#0A6464"/>   
  33.         <mx:Label x="10" y="171" id="item3" width="367" fontSize="14" color="#0A6464"/>   
  34.         <mx:Label x="37" y="30" text="請選者城市"/>   
  35.     </mx:Panel>   
  36. </mx:Application>  
<?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>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.