使用PHP和MySQL填充flex 4.5 DataGrid

时间:2011-09-28 20:26:37

标签: php mysql flex datagrid flex4.5

嘿所以我最近开始使用Flash Builder 4.5,因此在构建移动应用程序时遇到了很多愚蠢的问题,特别是在将我的应用程序连接到后端服务器时。

我目前正在处理的事情应该是更新DataGrid,并从MySQL数据库中检索这些数据。

此代码在浏览器上清楚地显示数据,但数据网格未更新。相反,它会抛出以下错误。

TypeError: Error #1034: Type Coercion failed: cannot convert mx.utils::ObjectProxy@1e43bc1 to mx.collections.IList.
    at views::connectPHPHomeView/userRequest_resultHandler()[C:\Documents and Settings\s4710935\Adobe Flash Builder 4.5\connectPHP\src\views\connectPHPHomeView.mxml:17]
    at views::connectPHPHomeView/__userRequest_result()[C:\Documents and Settings\s4710935\Adobe Flash Builder 4.5\connectPHP\src\views\connectPHPHomeView.mxml:29]
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at HTTPOperation/http://www.adobe.com/2006/flex/mx/internal::dispatchRpcEvent()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\rpc\http\HTTPService.as:993]
    at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::resultHandler()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\rpc\AbstractInvoker.as:318]
    at mx.rpc::Responder/result()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\rpc\Responder.as:56]
    at mx.rpc::AsyncRequest/acknowledge()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\rpc\AsyncRequest.as:84]
    at DirectHTTPMessageResponder/completeHandler()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\messaging\channels\DirectHTTPChannel.as:451]
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at flash.net::URLLoader/onComplete()

有人可以指导我完成这个吗?

修改

这是PHP代码:

  <?php

    // -----------------------------------------------------------------------------
    // Initialization of the sql server
    // -----------------------------------------------------------------------------


    // hostname or ip of server (for local testing, localhost should work)
    $dbServer='localhost';

    // username and password to log onto db server
    $dbUser='root';
    $dbPass='password';

    // name of database
    $dbName='test';

    mysql_connect("$dbServer", "$dbUser", "$dbPass") or die(mysql_error());
    mysql_select_db("$dbName") or die(mysql_error());


    //Creating the table and inserting the values in it...



    mysql_query("CREATE TABLE IF NOT EXISTS mariah_transaction_log(
    id INT NOT NULL AUTO_INCREMENT, 
      PRIMARY KEY(id),
        date DATE, 
        details VARCHAR(150),
        debit DOUBLE,
        credit DOUBLE,
        balance DOUBLE)")
    or die(mysql_error()); 


    mysql_query("INSERT INTO mariah_transaction_log 
        (date, details, debit, credit, balance) VALUES('2011-08-08','Zellers','12.56','0.00','12343.54') ") 
    or die(mysql_error());  


    $username = $_GET["username"];

    // $namedGreeting = mysql_query("SELECT * FROM customer_list WHERE username='$username'") or die(mysql_error());
    // $row = mysql_fetch_array( $namedGreeting );

    // echo "Hello ".$row['name']."!";


    $Query = "SELECT * from mariah_transaction_log"; 
    $Result = mysql_query( $Query ); 

    $Return = "<users>"; 

    while ( $User = mysql_fetch_object( $Result ) ) 
    { 
    $Return .= "<user><date>".$User->date."</date><details>". 
                $User->details."</details><debit>". 
                $User->debit."</debit><credit>".
                $User->credit."</credit><balance>".
                $User->balance."</balance></user>"; 
    } 
    $Return .= "</users>"; 
    mysql_free_result( $Result ); 
    print ($Return) 



    ?>

以下是我的代码的MXML和AS3部分: 注意 为简单起见,添加了示例XML结果

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" title="HomeView">

    <fx:Script>
        <![CDATA[
            import mx.collections.IList;
            import mx.collections.XMLListCollection;
            import mx.rpc.events.ResultEvent;






            protected function callService(event:ResultEvent):void
            {
                var results:XML = sampleResult;
                var resultsList:XMLListCollection = results.users as XMLListCollection;
                showTransactions_dg.dataProvider = resultsList;


            }

        ]]>
    </fx:Script>


    <fx:Declarations>


        <fx:XML id="sampleResult" xmlns="">
            <users>
                <user>
                    <date>2011-08-07</date>
                    <details>Camello Shop</details>
                    <debit>67.32</debit>
                    <credit>0.00</credit>
                    <balance>1233.42</balance>
                </user>

                <user>
                    <date>2011-08-07</date>
                    <details>Deposit</details>
                    <debit>0.00</debit>
                    <credit>600.00</credit>
                    <balance>1833.42</balance>
                </user>

            </users>



        </fx:XML>

        <s:HTTPService id="userRequest" url="{phpFile}"
                       method="GET" resultFormat="e4x" 
                       result="callService(event)" useProxy="false">
            <s:request xmlns="">
                <username>{username_txt.text}</username>
            </s:request>
        </s:HTTPService>


        <!-- The link to the php file -->
        <fx:String id="phpFile">http://localhost:8080/connectPHP/phpFile.php </fx:String>       

    </fx:Declarations>


    <s:TextInput id="username_txt" x="11" y="78"/>
    <s:Label x="11" y="55" text="Username"/>
    <s:Button id="getData_button" x="10" y="122" label="Done?" 
              click="userRequest.send()"/>
    <s:Label id="showName_lbl" x="13" y="173" text="show name here"/>


    <s:DataGrid id="showTransactions_dg" x="10" y="218" width="300" height="86">
        <s:columns>
            <s:ArrayCollection>
                <s:GridColumn dataField="date" headerText="Date" />
                <s:GridColumn dataField="details" headerText="Details"/>
                <s:GridColumn dataField="debit" headerText="Debit"/>
                <s:GridColumn dataField="credit" headerText="Credit"/>
                <s:GridColumn dataField="balance" headerText="Balance"/>

            </s:ArrayCollection>
        </s:columns>
    </s:DataGrid>
    <s:Label id="testLbl" x="13" y="334" width="266" height="90" text="Label"/>




</s:View>

1 个答案:

答案 0 :(得分:1)

spark DataGrid中,dataProvider必须实现iList接口。看起来您正在返回XML并将其存储在通用对象中。通用对象不支持IList接口。

您应该向HTTPService添加结果处理程序,而不是将远程调用的结果绑定到dataGrid的dataProvider:

<s:HTTPService id="userRequest" url="{phpFile}"
               method="GET" resultFormat="object" useProxy="false" result="onResult(event)">

然后将结果Handler中的数据转换为实现IList的内容。在这种情况下,我建议使用XMLListCollection。

protected function onResult(event:ResultEvent):void{
 var resultsXML : XML = event.results as XML
 showTransactions_dg.dataProvider = new XMLListCollection(resultsXML);
}

此代码是在浏览器中编写的,可能并不完美。