如何将 SharedObject 数组数据发送到 PHP WS 进行数据库查询 (Flex)

发布于 2024-11-26 21:32:44 字数 7283 浏览 1 评论 0原文

我目前正在使用 Flex 4.5.1 开发 Android 应用程序,在尝试将 SharedObject 数组中存储的数据传递到 Web 服务以进行数据库查询时遇到问题。下面的代码显示了我如何将数据存储在 SharedObject 中:

var so:SharedObject = SharedObject.getLocal("app");
            public var prefsArray:ArrayCollection = new ArrayCollection(so.data.prefs);

protected function prefs_btn_click(event:MouseEvent):void
            {

                prefsArray.source.push(getFrsByIDResult.lastResult.id);
                so.data.prefs = [prefsArray];

                var flushStatus:String = so.flush();
                if (flushStatus != null) {
                    switch(flushStatus) {
                        case SharedObjectFlushStatus.PENDING:
                            so.addEventListener(NetStatusEvent.NET_STATUS,
                                onFlushStatus);
                            break;
                        case SharedObjectFlushStatus.FLUSHED:
                        trace("success");
                        break;
                    }
                }
            }

            protected function onFlushStatus(event:NetStatusEvent):void 
            {           
                trace(event.info.code);
            }

我已经测试了 SharedObject,看看信息是否正确输入其中,一切看起来都很好。现在,我使用下面的代码从 SharedObject 检索数据并尝试将其发送到 PHP Web 服务以运行数据库查询。

var so:SharedObject = SharedObject.getLocal("app"); 
            var arrCol:ArrayCollection = new ArrayCollection(so.data.prefs);
            var str:String = new String(arrCol.toString());

            protected function list_creationCompleteHandler(event:FlexEvent):void
            {
                getPrefsByprefIdsResult.token = prefsService.getPrefsByPrefIds(so.data.prefs);          
            }

我已经在 Flex 中测试了 Web 服务,并将其配置为接收整数数组 (int[]),并且当我使用两个虚拟值对其运行测试操作时,它可以工作。但是,当我尝试使用上面的代码向 Web 服务传递共享对象数据时,我收到此错误:

TypeError: Error #1034: Type Coercion failed: cannot convert []@97e97e1 to mx.collections.ArrayCollection.
    at views::**************/list_creationCompleteHandler()[C:\Users\Jack\Adobe Flash Builder 4.5\****************\src\views\*******************.mxml:25]
    at views::*********************/__list_creationComplete()[C:\Users\Jack\Adobe Flash Builder 4.5\****************\src\views\***************.mxml:94]
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at mx.core::UIComponent/dispatchEvent()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\core\UIComponent.as:13128]
    at mx.core::UIComponent/set initialized()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\core\UIComponent.as:1818]
    at mx.managers::LayoutManager/validateClient()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\managers\LayoutManager.as:1090]
    at mx.core::UIComponent/validateNow()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\core\UIComponent.as:8067]
    at spark.components::ViewNavigator/commitNavigatorAction()[E:\dev\4.5.1\frameworks\projects\mobilecomponents\src\spark\components\ViewNavigator.as:1878]
    at spark.components::ViewNavigator/commitProperties()[E:\dev\4.5.1\frameworks\projects\mobilecomponents\src\spark\components\ViewNavigator.as:1236]
    at mx.core::UIComponent/validateProperties()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\core\UIComponent.as:8209]
    at mx.managers::LayoutManager/validateProperties()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\managers\LayoutManager.as:597]
    at mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\managers\LayoutManager.as:783]
    at mx.managers::LayoutManager/doPhasedInstantiationCallback()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\managers\LayoutManager.as:1180]

我已用 * 替换了某些文件名和位置以保护我正在做的工作,但是有人可以帮助我吗?问题,因为我相信它必须是简单的事情???

谢谢,


好的,让我更详细地解释一下。正如我所说,这是为 Android 应用程序设计的,但想象一下我想要做的是使用本地共享对象持久存储书签。

您在上面看到的第一块代码旨在为我想要创建的书签创建 LSO 属性,并想象可以在不同时间设置多个书签,就像在 Web 浏览器中一样。我能找到的唯一方法是将这些项目/详细信息存储在一个数组中,我检索该数组,然后更新,然后保存回 LSO 并保存。

第二段代码涉及想象一个“书签页面”,其中包含我已添加书签的所有内容的列表。现在我想要发生的是我能够调用保存书签 ID 的 LSO 属性,然后以列表格式加载其详细信息。

我已成功创建 LSO 并将书签详细信息存储在其中,并允许更新它们并添加条目。另外,我还确保我的 PHP 代码会拉回与 id 数组相关的所有数据库对象,并且已经使用 Flex 对其进行了测试。我似乎唯一不能做的就是将 id 传递给 PHP Web 服务文件。 Web 服务文件中的代码如下(如果有帮助的话):

public function getPrefsByPrefIds($PrefIds) {

      $stmt = mysqli_prepare($this->connection, "SELECT * FROM $this->tablename WHERE $this->tablename.id IN(" .implode(",", $PrefIds). ")");       
      $this->throwExceptionOnError();

      mysqli_stmt_execute($stmt);
      $this->throwExceptionOnError();

      $rows = array();
      mysqli_stmt_bind_result($stmt, $row->id, $row->name, $row->desc);

     while (mysqli_stmt_fetch($stmt)) {
          $rows[] = $row;
          $row = new stdClass();
          mysqli_stmt_bind_result($stmt, $row->id, $row->name, $row->desc);
        }

      mysqli_stmt_free_result($stmt);
      mysqli_close($this->connection);
      return $rows;
    }

是的,我已经尝试过了,但是谢谢。由于我一直在尝试可以存储在 SharedObjects 中的不同类型的对象,因此我自己取得了更多进展。我已经设法让解决方案部分使用此代码:

此代码旨在捕获 bomark 信息并将其存储在 arrayCollection 中,然后将其传输到 bytesArray 并保存

var so:SharedObject = SharedObject.getLocal("app");
            public var prefArray:ArrayCollection = new ArrayCollection(so.data.prefs);

            protected function prefs_btn_click(event:MouseEvent):void
            {               
                prefArray.source.push(getCompaniesByIDResult.lastResult.id);
                so.data.prefs = [prefArray];

                var bytes:ByteArray = new ByteArray();
                bytes.writeObject(prefArray);

                so.data.ac  = bytes;

                var flushStatus:String = so.flush();
                if (flushStatus != null) {
                    switch(flushStatus) {
                        case SharedObjectFlushStatus.PENDING:
                            so.addEventListener(NetStatusEvent.NET_STATUS,
                                onFlushStatus);
                            break;
                        case SharedObjectFlushStatus.FLUSHED:
                        trace("success");
                        break;
                    }
                }
            }

            protected function onFlushStatus(event:NetStatusEvent):void
            {           
                trace(event.info.code);
            }

下一个代码是设计用于从 SahredObjects bytesArray 检索该信息并将其放回到数组集合中

var so:SharedObject = SharedObject.getLocal("app");               
            var ba:ByteArray = so.data.ac as ByteArray;
            var ac:ArrayCollection;


            protected function list_creationCompleteHandler(event:FlexEvent):void
            {
                ba.position = 0;
                ac = ba.readObject() as ArrayCollection;
                getPrefsByPrefIdsResult.token = prefsService.getPrefsByPrefIds(ac);   
            }

但是正如我所说,这只是以一种很小的方式工作,就好像我只为一项存储一个书签(id),然后去到书签列表该书签的详细信息已成功检索,但是如果我保存多个书签(2 个或多个 id),该页面将不会加载详细信息,我没有收到错误,但我相信它挂起,因为它正在寻找说 id 的“ 1,2”而不是“1”和“2”,但我不知道这是为什么或如何解决这个问题。我很感激别人给我的建议,但我发现很难,没有人可以帮助我解决这个问题,我不得不对代码进行各种实验。有人可以帮我解决这个问题吗,我真的很感激:-)谢谢

I am currently developing an Android application using Flex 4.5.1 and I am having an issue when trying to pass data that I have stored in a SharedObject array to my Web Service for a Database query. the code below shows how I am storing the data in the SharedObject:

var so:SharedObject = SharedObject.getLocal("app");
            public var prefsArray:ArrayCollection = new ArrayCollection(so.data.prefs);

protected function prefs_btn_click(event:MouseEvent):void
            {

                prefsArray.source.push(getFrsByIDResult.lastResult.id);
                so.data.prefs = [prefsArray];

                var flushStatus:String = so.flush();
                if (flushStatus != null) {
                    switch(flushStatus) {
                        case SharedObjectFlushStatus.PENDING:
                            so.addEventListener(NetStatusEvent.NET_STATUS,
                                onFlushStatus);
                            break;
                        case SharedObjectFlushStatus.FLUSHED:
                        trace("success");
                        break;
                    }
                }
            }

            protected function onFlushStatus(event:NetStatusEvent):void 
            {           
                trace(event.info.code);
            }

I have tested the SharedObject to see if the information is being entered into it correctly and all seems fine. Now I have used the code below in order to retrieve the data from the SharedObject and try and send it to the PHP web Service to run the DB query.

var so:SharedObject = SharedObject.getLocal("app"); 
            var arrCol:ArrayCollection = new ArrayCollection(so.data.prefs);
            var str:String = new String(arrCol.toString());

            protected function list_creationCompleteHandler(event:FlexEvent):void
            {
                getPrefsByprefIdsResult.token = prefsService.getPrefsByPrefIds(so.data.prefs);          
            }

I have tested the Webservice in Flex and have it configured to recieve an Array of Ints (int[]) and it works when i run a test operation on it with two dummy values. However when I try to use the code above to pass the Web Service the Shared Object data I get this error:

TypeError: Error #1034: Type Coercion failed: cannot convert []@97e97e1 to mx.collections.ArrayCollection.
    at views::**************/list_creationCompleteHandler()[C:\Users\Jack\Adobe Flash Builder 4.5\****************\src\views\*******************.mxml:25]
    at views::*********************/__list_creationComplete()[C:\Users\Jack\Adobe Flash Builder 4.5\****************\src\views\***************.mxml:94]
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at mx.core::UIComponent/dispatchEvent()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\core\UIComponent.as:13128]
    at mx.core::UIComponent/set initialized()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\core\UIComponent.as:1818]
    at mx.managers::LayoutManager/validateClient()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\managers\LayoutManager.as:1090]
    at mx.core::UIComponent/validateNow()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\core\UIComponent.as:8067]
    at spark.components::ViewNavigator/commitNavigatorAction()[E:\dev\4.5.1\frameworks\projects\mobilecomponents\src\spark\components\ViewNavigator.as:1878]
    at spark.components::ViewNavigator/commitProperties()[E:\dev\4.5.1\frameworks\projects\mobilecomponents\src\spark\components\ViewNavigator.as:1236]
    at mx.core::UIComponent/validateProperties()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\core\UIComponent.as:8209]
    at mx.managers::LayoutManager/validateProperties()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\managers\LayoutManager.as:597]
    at mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\managers\LayoutManager.as:783]
    at mx.managers::LayoutManager/doPhasedInstantiationCallback()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\managers\LayoutManager.as:1180]

I have replaced certain filenames and locations with *'s to protect the work i am doing, but can someone please help me with this issues as I believe it has to be something simple???

Thanks


ok so let me explain in more detail. This is being designed for an Android app like I said, but image what I am trying to do is to store Bookmarks persistently using the Local Shared Object.

The first chunck of code you see above is designed to create the LSO attribute for the bookmark i want to create and imagine that there can be more than one bookmark set at different times like in a web browser. The only way i could find to do this was to store these items/details in an array which I retrieve and then update before saving back to the LSO and saving.

The second piece of code related to imagine a "Bookmarks Page" with a list of all the content that I have bookmarked. Now what I wanted to happen was thta I would be able to call up the LSO attribute which held the id's of the bookmarks and then load up thier details in a list format.

I have managed to create the LSO and store the bookmark deatils in and allow them to be updated and entries added. Also I have made sure that the PHP code that I have pulls back all the database objects relating to the array of id's and this has been tested using flex. The only thing that I cant seem to do is to pass the id's to the PHP web service file. The code in the Web Service file is below if that helps:

public function getPrefsByPrefIds($PrefIds) {

      $stmt = mysqli_prepare($this->connection, "SELECT * FROM $this->tablename WHERE $this->tablename.id IN(" .implode(",", $PrefIds). ")");       
      $this->throwExceptionOnError();

      mysqli_stmt_execute($stmt);
      $this->throwExceptionOnError();

      $rows = array();
      mysqli_stmt_bind_result($stmt, $row->id, $row->name, $row->desc);

     while (mysqli_stmt_fetch($stmt)) {
          $rows[] = $row;
          $row = new stdClass();
          mysqli_stmt_bind_result($stmt, $row->id, $row->name, $row->desc);
        }

      mysqli_stmt_free_result($stmt);
      mysqli_close($this->connection);
      return $rows;
    }

Yes I had already tried that but thanks. I have made some more progress on my own as I have been experimenting with the different types of objects that can be stored in SharedObjects. I have managed to get the solution part working with this code:

This code is designed to capture the boomark info and store it in an arrayCollection before transferring it to a bytesArray and saving

var so:SharedObject = SharedObject.getLocal("app");
            public var prefArray:ArrayCollection = new ArrayCollection(so.data.prefs);

            protected function prefs_btn_click(event:MouseEvent):void
            {               
                prefArray.source.push(getCompaniesByIDResult.lastResult.id);
                so.data.prefs = [prefArray];

                var bytes:ByteArray = new ByteArray();
                bytes.writeObject(prefArray);

                so.data.ac  = bytes;

                var flushStatus:String = so.flush();
                if (flushStatus != null) {
                    switch(flushStatus) {
                        case SharedObjectFlushStatus.PENDING:
                            so.addEventListener(NetStatusEvent.NET_STATUS,
                                onFlushStatus);
                            break;
                        case SharedObjectFlushStatus.FLUSHED:
                        trace("success");
                        break;
                    }
                }
            }

            protected function onFlushStatus(event:NetStatusEvent):void
            {           
                trace(event.info.code);
            }

This next code is the designed to retrieve that information from the SahredObjects bytesArray and put it back into an Array Collection

var so:SharedObject = SharedObject.getLocal("app");               
            var ba:ByteArray = so.data.ac as ByteArray;
            var ac:ArrayCollection;


            protected function list_creationCompleteHandler(event:FlexEvent):void
            {
                ba.position = 0;
                ac = ba.readObject() as ArrayCollection;
                getPrefsByPrefIdsResult.token = prefsService.getPrefsByPrefIds(ac);   
            }

however as I have said this works in a small way only as if I store only one Bookmark (id) for an item and then go to the bookmarks list the details for that bookark are successfully retrieved, however if I save more than one Bookmark(2 or more id's) the page will not load the details, i do not get an error but I believe it is hanging because it is looking for say id's "1,2" instead of "1" and "2" but i dont know why this is or how to resolve this. I appreciate the advice I have been given but am finding it hard there is no one who can help me with this issue and I am having to do various experiemnts with the code. Can someone please help me with this I would really appreciate it :-) Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文