溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          雪山飛鵠

          溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
          偶爾寫寫php感覺心情還是蠻舒暢的(Java里的Struts+Hibernate+Spring寫久了),寫寫php才知道,這種被解放的感覺真好。不得不說,php是一種服務器端比較精辟的語言,難怪崇拜者這么多。就來整整flex基于php的交互,看好了,這里要介紹的不是通過flex里面的HttpService組件與php交互,而是借助AMFPHP通過RemoteObject方式來交互。
          關于amfphp環境的搭建,請參考本人寫的amfphp環境搭建教程,當然里面寫的比較粗略,有不清粗的可以聯系我。
          先來看看php端代碼
          ProductServices.php
          <?php
          class ProductServices{
              
          /**
              *query product list
              
          */
              
          function getProductList(){
                  
          $link=@mysql_connect("localhost", "root", "") or die("Could not connect");
                  
          mysql_select_db("compass",$link);
                  
          mysql_query("set names utf8",$link);
                  
          $result = mysql_query("SELECT * FROM product",$link);
                  
          $array=array();
                  
          while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                      
          array_push($array,$row);
                  }
                  
          mysql_free_result($result);
                  
          mysql_close($link);
                  
          return $array;
              }



              
          function findProductById($id){
                  
          $link=@mysql_connect("localhost", "root", "") or die("Could not connect");
                  
          mysql_select_db("compass",$link);
                  
          mysql_query("set names utf8",$link);
                  
          $result = mysql_query("SELECT * FROM product where id= ".$id,$link);
                  
          $array=array();
                  
          while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                      
          array_push($array,$row);
                  }
                  
          mysql_free_result($result);
                  
          mysql_close($link);
                  
          return $array;
              }

          }
          ?>

          在ProductServices.php文件中,定義了一個類ProductServices,里面封裝了兩個方法,getProductList(),findProductById($id)里面內容很簡單,一個是全部查詢商品,一個是根據Id查詢商品

          注意該文件存放的位置C:\inetpub\wwwroot\amfphp\services\ 這樣可以被amfphp的資源管理器檢索到
           


          編寫flex端代碼
          <?xml version="1.0" encoding="utf-8"?>
          <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                         xmlns:s
          ="library://ns.adobe.com/flex/spark" 
                         xmlns:mx
          ="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
                         creationComplete
          ="ro.getOperation('getProductList').send()"
                         
          >
              
          <!-- 
                  ro.getOperation('getProductList').send() 
                  ro為RemoteObject的Id
                  ro.getOperation('getProductList')獲取php文件中的方法名,及要調用服務器端的那個方法
                  send()發送請求,在send中可傳遞參數,多個參數之間用逗號分隔,參數名要與服務器端的參數名一致
              
          -->
              
          <fx:Declarations>
                  
          <!-- 將非可視元素(例如服務、值對象)放在此處 -->
                  
                  
          <s:RemoteObject id="ro" 
                                  destination
          ="amfphp"  
                                  source
          ="ProductServices" 
                                  fault
          ="getProductList_faultHandler(event)" 
                                  result
          ="getProductList_resultHandler(event)"
                                  endpoint
          ="http://192.168.3.11/amfphp/gateway.php">
                  
          </s:RemoteObject>
                  
                  
          <!--
                      RemoteObject中的destination需要與src目錄下的services
          -config.xml中定義的destination的Id保持一致
                      source
          ="ProductServices"要調用服務器端的那個php類,如果存在包的話注意包名.類名
                      fault 失敗時響應的方法
                      result 成功時的方法
                      endpoint
          ="http://192.168.3.11/amfphp/gateway.php" 正確訪問gateway.php的地址
                  
          -->
                  
              
          </fx:Declarations>
              
              
          <fx:Script>
                  
          <![CDATA[
                      import mx.collections.ArrayCollection;
                      import mx.controls.Alert;
                      import mx.rpc.events.FaultEvent;
                      import mx.rpc.events.ResultEvent;
                      import mx.utils.ArrayUtil;
                      
                      [Bindable]
                      internal 
          var dp:ArrayCollection;
                      
                      
                      
                      
          //amfphp請求成功時調用方法
                      protected function getProductList_resultHandler(event:ResultEvent):void
                      {
                          dp
          =new ArrayCollection(ArrayUtil.toArray(event.result));
                      }
                      
          //amfphp請求失敗時調用方法
                      protected function getProductList_faultHandler(event:FaultEvent):void
                      {
                          Alert.show(
          "失敗了",event.fault.message);                
                      }
                      
                  ]]
          >
              
          </fx:Script>
              
              
              
          <s:layout>
                  
          <s:HorizontalLayout/>
              
          </s:layout>
              
          <s:DataGrid width="519" height="292" dataProvider="{dp}" requestedRowCount="4">
                  
          <s:columns>
                      
          <s:ArrayList>
                          
          <s:GridColumn dataField="id" headerText="編號"></s:GridColumn>
                          
          <s:GridColumn dataField="name" headerText="商品名稱"></s:GridColumn>
                          
          <s:GridColumn dataField="price" headerText="單價"></s:GridColumn>
                          
          <s:GridColumn dataField="descption" headerText="描述"></s:GridColumn>
                      
          </s:ArrayList>
                  
          </s:columns>
              
          </s:DataGrid>
              
          </s:Application>

          必須在flex工程的src目錄下存放一個名為services-config.xml
          <? version="1.0" encoding="UTF-8"?>
          <services-config>
              
          <services>
                  
          <service id="sabreamf-flashremoting-service"
                           class
          ="flex.messaging.services.RemotingService"
                           messageTypes
          ="flex.messaging.messages.RemotingMessage">
                      
          <destination id="amfphp">
                          
          <channels>
                              
          <channel ref="my-amfphp"/>
                          
          </channels>
                          
          <properties>
                              
          <source>*</source>
                          
          </properties>
                      
          </destination>
                  
          </service>
              
          </services>

              
          <channels>
                  
          <channel-definition id="my-amfphp" class="mx.messaging.channels.AMFChannel">
                      
          <endpoint uri="http://192.168.3.11/amfphp/gateway.php" class="flex.messaging.endpoints.AMFEndpoint"/>
                  
          </channel-definition>
              
          </channels>
          </services-config>

          需要將該文件編譯到環境中去

          效果圖

          點我下載代碼
          posted on 2011-10-28 11:52 雪山飛鵠 閱讀(2203) 評論(0)  編輯  收藏 所屬分類: flex+php
          主站蜘蛛池模板: 和政县| 兰溪市| 阿拉善右旗| 浙江省| 安达市| 屏东县| 阳江市| 师宗县| 衡南县| 厦门市| 兴仁县| 溧阳市| 古浪县| 尼勒克县| 依安县| 忻城县| 邢台县| 孙吴县| 沁阳市| 乡城县| 江安县| 柘城县| 开平市| 合江县| 田阳县| 临朐县| 化隆| 寻甸| 贵定县| 绥宁县| 天津市| 清徐县| 湖南省| 仙居县| 子洲县| 修水县| 洞口县| 赫章县| 视频| 武定县| 称多县|