CikLinas
CikLinas

Reputation: 242

Reading objects from webservice actionscript

I need to read objects and save them in array. I did that on c# but can't figure out how to do that on actionscript.

c# example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestingWSDLLOad.ServiceReference2;

namespace TestingWSDLLOad
{
    class Program
    {



        static void Main(string[] args)
        {
            ServiceReference2.Service1Client testas = new ServiceReference2.Service1Client();
            SortedList<int, PlayListItem> playList = new SortedList<int, PlayListItem>();
            int cc = 0;
            foreach (var resultas in testas.GetPlayList(394570))
            {

                PlayListItem ss = new PlayListItem(resultas.Id, resultas.VideoId, resultas.Path);
                playList.Add(cc, ss);
                cc++;

            }


            Console.WriteLine(playList[0].Id);


            Console.ReadKey();
        }
    }

    public class PlayListItem
    {
        public int VideoId { get; private set; }
        public string Path { get; private set; }
        public int Id { get; private set; }


        public PlayListItem(int id, int videoId, string path)
        {
            Id = id;
            VideoId = videoId;
            Path = path;
        }

    }
}

I know how to get a simple result from wsdl using actionscript, but don't know how to get objects with parameteres and save them.

Service has a method GetPlaylist(int value) which returns an array of objects (id, videoId, path). How to handle this and save them ?

Here is my as3:

package {


    public class data extends CasparTemplate {

        var flvControl:FLVPlayback;
        var refreshTimer:Timer;

        var videoList:Array;
        var videoNew:Array;

        var videoMaxIds:Array;
        var videoNewMaxIds:Array;

        var videoIndex:uint;
        var videoIdFrom:uint;

        var loopAtEnd:Boolean;

        var _playListItems:Array;
        var _playList:PlayListItem;
        var gotNewPlaylist:Boolean;

        var webService:WebService;
        var serviceOperation:AbstractOperation;

        public function data() 
        {
            _playListItems = new Array();
            flvControl = new FLVPlayback();
            videoNew = new Array();
            videoNewMaxIds = new Array();
            videoIndex = 0;
            videoIdFrom = videoMaxIds[videoIndex];

            loopAtEnd = true;
            gotNewPlaylist = false;

            refreshTimer = new Timer(20000);
            refreshTimer.addEventListener(TimerEvent.TIMER, getNewPlaylist);
            refreshTimer.start();

            flvControl.addEventListener(VideoEvent.COMPLETE, completeHandler);
            flvControl.addEventListener(VideoEvent.STATE_CHANGE, vidState);
            flvControl.setSize(720, 576);
            flvControl.visible = true;
            //addChild(flvControl);

            var url:String = "http://xxx/yyy.svc?wsdl";
            webService = new WebService();
            webService.loadWSDL(url);
            webService.addEventListener(LoadEvent.LOAD, BuildServiceRequest);

        }

        function BuildServiceRequest(evt:LoadEvent):void
        {

            webService.removeEventListener(LoadEvent.LOAD, BuildServiceRequest);

            //serviceOperation.addEventListener(ResultEvent.RESULT, displayResult);

            for (var resultas in webService.getOperation("GetPlaylist(394575)"))
            {
                 trace(resultas.Id);
            }
            //serviceOperation = webService.getOperation("GetPlaylist");
            //serviceOperation.arguments[{videoId: "394575"}];

        } 

        private function displayResult(e:ResultEvent):void
        {
            trace("sss");
        }



        // Handle the video completion (load the next video)
        function completeHandler(event:VideoEvent):void
        {
           if (gotNewPlaylist)
           {
               videoList = videoNew;
               videoMaxIds = videoNewMaxIds;

               videoNew = null;
               videoNewMaxIds = null;
               gotNewPlaylist = false;
               videoIndex = 0;
           } else
             videoIndex++;

           nextVideo();
        }

        private function vidState(e:VideoEvent):void {

            var flvPlayer:FLVPlayback = e.currentTarget as FLVPlayback;
            if (flvPlayer.state==VideoState.CONNECTION_ERROR)   {
                trace("FLVPlayer Connection Error! -> path : "+flvPlayer.source);
                videoIndex++;
                nextVideo();
            } else if (flvPlayer.state==VideoState.DISCONNECTED)    {
                videoIndex++;
                nextVideo();
            }
        }

        function nextVideo():void
        {   
            trace("Video List:"+videoList.toString());
            if( videoIndex == videoList.length ){
               if( loopAtEnd )
               {
                    videoIndex = 0;
               } else { return; }
           }

           flvControl.source = videoList[videoIndex];
           if (videoIdFrom < videoMaxIds[videoIndex])
                videoIdFrom = videoMaxIds[videoIndex];
           trace(videoIdFrom);  
        }

    }
}

internal class PlayListItem
{
    private var _videoId:int;
    private var _path:String;
    private var _id:int;

    public function get VideoId():int { return _videoId; }
    public function get Path():String { return _path; }
    public function get Id():int { return _id; }

    public function set VideoId(setValue:int):void { _videoId = setValue };
    public function set Path(setValue:String):void { _path = setValue };
    public function set Id(setValue:int):void { _id = setValue };

    public function PlayListItem(id:int, videoId:int, path:String)
    {
        _videoId = videoId;
        _id = id;
        _path = path;

    }// end function

}

Upvotes: 0

Views: 56

Answers (1)

Atriace
Atriace

Reputation: 2558

I think you were on the right track with your commented-out code. Be aware that the getOperation() will return an AbstractOperation, which in my mind is simply a pointer to the remote function. You can set arguments on the object, or simply pass the arguments when you call send(). I know some people have had issues with the argument property approach, so simply passing your arguments in the send function may be the smart way to go.

The following replace BuildServiceRequest and displayResult

private function BuildServiceRequest(evt:LoadEvent):void {
    webService.removeEventListener(LoadEvent.LOAD, BuildServiceRequest);
    serviceOperation.addEventListener(ResultEvent.RESULT, displayResult);
    serviceOperation = webService.getOperation("GetPlaylist");
    serviceOperation.send(394575);
}

private function displayResult(e:ResultEvent):void {
    // Store the token as our array.
    _playListItems = e.token;
    var msg:String;

    // Loop through the array
    for each (var entry:Object in _playListItems) {
        msg = "";
        // For every key:value pair, compose a message to trace
        for (var key:String in entry) {
            msg += key + ":" + entry[key] + "   ";
        }
        trace(msg);
    }
}

Upvotes: 1

Related Questions