• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
Locked
0

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

New Here ,
Jul 27, 2011 Jul 27, 2011

Copy link to clipboard

Copied

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()
    at views::*********************/__list_creationComplete()
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at mx.core::UIComponent/dispatchEvent()
    at mx.core::UIComponent/set initialized()
    at mx.managers::LayoutManager/validateClient()
    at mx.core::UIComponent/validateNow()
    at spark.components::ViewNavigator/commitNavigatorAction()
    at spark.components::ViewNavigator/commitProperties()
    at mx.core::UIComponent/validateProperties()
    at mx.managers::LayoutManager/validateProperties()
    at mx.managers::LayoutManager/doPhasedInstantiation()
    at mx.managers::LayoutManager/doPhasedInstantiationCallback()
[/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

Views

999

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 27, 2011 Jul 27, 2011

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 28, 2011 Jul 28, 2011

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jul 28, 2011 Jul 28, 2011

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 28, 2011 Jul 28, 2011

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jul 28, 2011 Jul 28, 2011

Copy link to clipboard

Copied

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...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jul 28, 2011 Jul 28, 2011

Copy link to clipboard

Copied

A string or an XML...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 28, 2011 Jul 28, 2011

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jul 28, 2011 Jul 28, 2011

Copy link to clipboard

Copied

Did you try

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 28, 2011 Jul 28, 2011

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines