9 Replies Latest reply on Jul 28, 2011 9:06 AM by anixan

    Send SharedObject Array data to PHP WS for DB Query?!? [Andorid]

    anixan

      Hi,

       

      I am currently working on 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 SharedObkject array to my Web Service for a Database query. the code below shows how I am storing the data in the SharedObject:

       

      [AS]
      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);
                  }[/AS]

       

      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.

       

      [AS]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);           
                  }[/AS]

       

      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:

       

      [AS]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\co re\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\sr c\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\mobil ecomponents\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\framewor k\src\mx\managers\LayoutManager.as:597]
          at mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\4.5.1\frameworks\projects\frame work\src\mx\managers\LayoutManager.as:783]
          at mx.managers::LayoutManager/doPhasedInstantiationCallback()[E:\dev\4.5.1\frameworks\projec ts\framework\src\mx\managers\LayoutManager.as:1180]
      [/AS]

       

      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

        • 1. Re: Send SharedObject Array data to PHP WS for DB Query?!? [Andorid]
          anixan Level 1

          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;
              }

          • 2. Re: Send SharedObject Array data to PHP WS for DB Query?!? [Andorid]
            anixan Level 1

            I cant believe that there is no one who can help me, can anyone tell me how i could store bookmarked id's of content in another way to be sent to my php web service so that i can have this functionality?

             

            Thanks

            • 3. Re: Send SharedObject Array data to PHP WS for DB Query?!? [Andorid]
              Rangora Level 3

              Can you tell me what is the line 25 (the error occurs at line 25)

              • 4. Re: Send SharedObject Array data to PHP WS for DB Query?!? [Andorid]
                anixan Level 1

                This is at line 25:

                 

                getPrefsByprefIdsResult.token = prefsService.getPrefsByPrefIds(so.data.prefs);

                 

                I have also tried first storing the SO data in a new ArrayCollection object and then passing that to the web service as the parameter but get the same error.

                 

                Thanks

                • 5. Re: Send SharedObject Array data to PHP WS for DB Query?!? [Andorid]
                  Rangora Level 3

                  A workaround would be to create a string from your array and send this string to the web service (you would have to change your web service declaration and your php script a bit too)... It's what I do in my app and I have no errors...

                  • 7. Re: Send SharedObject Array data to PHP WS for DB Query?!? [Andorid]
                    anixan Level 1

                    I have just tried to test the web service to make sure that it would accept a standard Array Collection and retrieve the data so I altered the code as shown below:

                     

                    var array1:ArrayCollection = new ArrayCollection( [1, 2, 3] );
                                           
                                protected function list_creationCompleteHandler(event:FlexEvent):void
                                {
                                    so.addEventListener(ResultEvent.RESULT, soLoaded);
                                    getPrefsByPrefIdsResult.token = prefsService.getPrefsByPrefIds(so.data.prefs);   
                                }

                     


                    When i changed the code to this the ArrayCollection was passed successfully to the Web Service and everything was fine. Now I am 100% sure that this issue is to do with the way the SharedObject is storing the ArrayCollection info, can anyone please help me in trying to resolve this by either telling me how I can store and retrieve an ArrayCollection from the SharedObject and pass it to the web servicesuccesfully or how I can do what I want to do in another way??

                     

                    Thanks

                    • 8. Re: Send SharedObject Array data to PHP WS for DB Query?!? [Andorid]
                      Rangora Level 3

                      Did you try

                       

                      getPrefsByprefIdsResult.token = prefsService.getPrefsByPrefIds(new ArrayCollection(so.data.prefs));     ?

                      • 9. Re: Send SharedObject Array data to PHP WS for DB Query?!? [Andorid]
                        anixan Level 1

                        Hi,

                         

                        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