Sealyu

          --- 博客已遷移至: http://www.sealyu.com/blog

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            618 隨筆 :: 87 文章 :: 225 評論 :: 0 Trackbacks
          這兩天在學習Google AppEngine的時候, 需要寫一個小程序,就是簡單的從數據庫里讀出數據,然后顯示出來。
          界面使用flex的,所以數據通信是想到了用amf,搜索到了兩個python的開源產品--pyAmf和Django Amf,后來看到Django Amf更新的并不頻繁,所以最后選了pyAmf。

          pyAmf還有一個優點是官方網站推薦了幾篇django與flex通信的文章里有一篇是中文的,哈哈。。。我喜歡中文。。。鏈接見
          http://blog.eshangrao.com/index.php/2008/02/16/447-flexpyamfdjango

          我的程序參考的也就是這篇文章,但代碼寫完后,程序并沒有通過,而是一直報找不到服務,折騰了兩天終于搞出來了。

          Django 服務端,

          1. 新建立一個工程testAMF,然后建立一個app--dailystory.

          2. 修改settings.py
             將dailystory添加到INSTALLED_APPS中。
             配置數據庫。

          3. 工程目錄中( whatidisplay 文件夾里 )的urls.py 的相關代碼如下:
          Python代碼
          1. from django.conf.urls.defaults import *  
          2. import settings  
          3.   
          4. urlpatterns = patterns('',  
          5.     (r'^dailystory/', include('testAMF.dailystory.urls')),  
          6.     (r'^admin/', include('django.contrib.admin.urls')),  
          7.     )  


          4. 在dailystory文件夾中添加一個urls.py文件
          Python代碼
          1. from django.conf.urls.defaults import *  
          2.   
          3. urlpatterns = patterns('',  
          4.     (r'^gateway/$', 'testAMF.dailystory.amfgateway.storyGateway'),  
          5.     (r'^(.*)$', 'django.views.static.serve', {'document_root':'dailystory/flex'}),  
          6.     )  


          5. 修改dailystory文件夾中的models.py 文件
          Python代碼
          1. from django.db import models  
          2.   
          3. class DailyStory(models.Model):  
          4.     content = models.TextField()  
          5.   
          6.     def __unicode__(self):  
          7.         return self.title  
          8.   
          9.     class Admin:  
          10.         pass  

          6. 在dailystory文件夾中添加一個amfgateway.py文件
          Python代碼
          1. from pyamf.remoting.gateway.django import DjangoGateway  
          2. from dailystory.models import DailyStory  
          3.   
          4. def getDailyStory(request):  
          5.     return DailyStory.objects.all()  
          6.   
          7.   
          8. storyGateway = DjangoGateway({  
          9.     'dailystory.getDailyStory': getDailyStory,  
          10.  })  

          Django的服務端就大功告成了


          Flex端更簡單
          Java代碼
          1. <?xml version="1.0" encoding="utf-8"?>  
          2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"  
          3.     layout="absolute" creationComplete="initApp();">  
          4.       
          5.     <mx:Script>  
          6.         <![CDATA[  
          7.             import flash.events.NetStatusEvent;  
          8.               
          9.             import mx.collections.ArrayCollection;  
          10.             import mx.rpc.AsyncToken;  
          11.             import mx.rpc.AsyncResponder;  
          12.             import mx.rpc.events.FaultEvent;  
          13.             import mx.rpc.events.ResultEvent;             
          14.               
          15.             private function initApp():void  
          16.             {  
          17.                   
          18.                     var token:AsyncToken=djangoService.getDailyStory();  
          19.                 token.addResponder(new AsyncResponder(onGetDailyStory,faultHandler));  
          20.             }  
          21.               
          22.             private function onGetDailyStory(re:ResultEvent, token:Object=null):void  
          23.             {  
          24.                 var stories:Array = re.result as Array;  
          25.                 var storyCollection:ArrayCollection = new ArrayCollection(stories);  
          26.                 yesterdayStory.text = storyCollection.getItemAt(0).content;  
          27.             }  
          28.               
          29.             private function faultHandler(fe:FaultEvent, token:Object=null):void  
          30.             {  
          31.                 trace(fe.fault.faultDetail);  
          32.             }  
          33.         ]]>  
          34.     </mx:Script>  
          35.       
          36.     <mx:RemoteObject  
          37.         id="djangoService"  
          38.         destination="dailystory"  
          39.         showBusyCursor="true">  
          40.     </mx:RemoteObject>  
          41.       
          42.     <mx:TextArea id="yesterdayStory" x="582" y="130" height="118" width="242"/>  
          43. </mx:Application>  


          然后需要在于與上述flex文件同目錄中新建一個services-config.xml
          Xml代碼
          1. <?xml version="1.0" encoding="UTF-8"?>  
          2. <services-config>  
          3.     <services>  
          4.         <service id="dailyStoryService" class="flex.messaging.services.RemotingService" messageTypes="flex.messaging.messages.RemotingMessage">  
          5.             <destination id="dailystory">  
          6.                 <channels>  
          7.                     <channel ref="dailyStoryChannel"/>  
          8.                 </channels>  
          9.                 <properties>  
          10.                     <source>*</source>  
          11.                 </properties>  
          12.             </destination>  
          13.         </service>  
          14.     </services>  
          15.     <channels>  
          16.         <channel-definition id="dailyStoryChannel" class="mx.messaging.channels.AMFChannel">  
          17.             <endpoint uri="http://localhost:8080/dailystory/gateway/" class="flex.messaging.endpoints.AMFEndpoint"/>  
          18.         </channel-definition>  
          19.     </channels>  
          20. </services-config>  


          在編譯時注意添加編譯參數-services services-config.xml

          然后將編譯后生成的在bin-debug文件夾中的文件復制到whatidisplay"dailystory"flex文件夾里

          運行django服務器
          Java代碼
          1. manage.py runserver 8080  

          訪問http://localhost:8080/dailystory/DailyStory.html



          這里與我參考的文章不同的有兩點,估計是pyAMF的更新造成的。我用的pyamf版本是0.3.1.
          第一個是在amfgateway.py文件中
          Java代碼
          1. storyGateway = DjangoGateway({  
          2.     'dailystory.getDailyStory': getDailyStory,  
          3.  })  

          我在給DjangoGateway賦值時使用了appName.method--即dailystory.getDailyStory
          據官方說這可能是個bug;)具體參看http://www.pyamf.org/wiki/DjangoHowto中的May be some problems here

          另外一處是 services-config.xml 文件中,我的destination id 給的就是django中App的名字。如果起其他名字的話,最后flex總是得不到服務。很奇怪。
          posted on 2009-05-01 23:36 seal 閱讀(670) 評論(0)  編輯  收藏 所屬分類: Flex+ActionScript
          主站蜘蛛池模板: 宝坻区| 刚察县| 科技| 含山县| 景谷| 上饶市| 邢台市| 桐庐县| 雅安市| 阿瓦提县| 太仓市| 襄城县| 江门市| 洪雅县| 剑阁县| 惠来县| 巩义市| 开平市| 卫辉市| 泰和县| 抚顺市| 基隆市| 老河口市| 枣阳市| 军事| 巴东县| 和龙市| 大埔区| 尚志市| 渝北区| 同江市| 茂名市| 元朗区| 龙井市| 安阳市| 濮阳县| 海丰县| 阆中市| 苍梧县| 商城县| 仙游县|