Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / Flash / Data Integration / August 2005



Tip: Looking for answers? Try searching our database.

Binding .NET WebService methods to Actionscript arrays

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dimitar - 25 Aug 2005 04:14 GMT
I can't find a way to fix this.
1. The scenario: A web service is reading the nodes of an XML file and
represting them into arrays.
2. A Flash UI is binding to these webservice methods (which return arrays) to
Actionscript arrays.
------
Here is the code:
1. The XML structure:
<songs>
<song><label>Song1</label><data>mp3/song1.mp3</data></song>
<song><label>Song2</label><data>mp3/song2.mp3</data></song>
</songs>

2. The webservice method:

 [WebMethod(Description = "Get the song title", CacheDuration = 1)]
    public String[] GetMp3SongLabel() {
        ArrayList Mp3SongLabel_arraylist = new ArrayList();
        XmlTextReader reader = new
XmlTextReader(http://localhost/mp3/mp3.xml");
        reader.WhitespaceHandling = WhitespaceHandling.None;
        while (reader.Read()) {
            if (reader.Name == "label") {
                Mp3SongLabel_arraylist.Add(reader.ReadString());
            }
        }
        reader.Close();
        String[] Mp3SongLabel_array = (String[])
Mp3SongLabel_arraylist.ToArray(typeof(String));
        return Mp3SongLabel_array;
    }

3. The AS code:

import mx.services.WebService;

//creating the webservice
var Mp3Player_service:WebService = new
WebService("http://localhost/Mp3WebService.asmx?wsdl");
Mp3Player_service.onFault = function(fault) {
    //show "Can't find the XML file"
    Main.Mp3Player.Status_txt.text = "Can't find the XML file";
    classes.GlobalClass.Mp3PlayerStatusText_str = Main.Mp3Player.Status_txt.text;
    //trace the actual WebService error, generated automatically from Flash Player
    trace(fault.faultstring);
}
var SongLabel_array:Array = new Array(Mp3Player_service.GetMp3SongLabel());
trace(SongLabel_array.length);

The Problem:
-> the trace function returns 1 as a length. The webservice method itself
bound to a aspx page returns the actual array. Similar binding to an
actionscript array doesn't do anything.

Any Solution?

------
Note: Please no examples and links for Flash Remoting, FlashOrb, etc. And no
examples with WebServiceConnector and visual binding.

I would appreciate pure code, in pure developer's way. If anyone could help,
will be great
------

Thanks,
Dimitar
LBStorm - 25 Aug 2005 19:39 GMT
This code was generated in about 10 seconds from the free code generator
available at www.DoneInAFlash.com :

AS:
      Result_obj = ws.Get_Customers();
     ws.onLoad = trace("Loading...");
     Result_obj.onResult = function(result) {
                 DG_ary=[];
                 for (i=0;i<result.length;i++){DG_ary.push(result);}
     }

.NET Web Service:

         Public Class Customers
            Public ContactName As String
            Public CompanyName As String
            Public CustomerID As String
      End Class

<WebMethodAttribute(Description:=" Get Customers")> Public Function
Get_Customers() As Customers()
    Dim myCommand As New OleDbCommand("select * from [Customers]", myConnection)
    myConnection.Open()
    Dim myDataAdapter As New OleDbDataAdapter
    myDataAdapter.SelectCommand = myCommand
    Dim myDataSet As New DataSet
    myDataAdapter.Fill(myDataSet)
    myConnection.Close()
    Dim x As Integer
    Dim CustomersArray As Customers() = New
Customers(myDataSet.Tables(0).Rows.Count-1) {}
    For x = 0 To myDataSet.Tables(0).Rows.Count - 1
           CustomersArray(x) = New Customers
                             CustomersArray(x).ContactName =
nz(myDataSet.Tables(0).Rows(x).Item("ContactName"))
           CustomersArray(x).CompanyName =
nz(myDataSet.Tables(0).Rows(x).Item("CompanyName"))
           CustomersArray(x).CustomerID =
nz(myDataSet.Tables(0).Rows(x).Item("CustomerID"))
    Next
   Return CustomersArray
Dimitar - 26 Aug 2005 00:07 GMT
thanks,

I'll check it out.

Anyway I found a way, fixing slightly the web service code as:

[WebMethod(Description = "Get the song title", CacheDuration = 1)]
    public String[] GetMp3SongLabel() {
        ArrayList Mp3SongLabel_arraylist = new ArrayList();
        int i = 0;
        String[] my_array = new String[10];
        XmlTextReader reader = new XmlTextReader(Mp3Dir + "mp3.xml");
        reader.WhitespaceHandling = WhitespaceHandling.None;
        while (reader.Read()) {
            if (reader.Name == "label") {
                //Mp3SongLabel_arraylist.Add(reader.ReadString());
                my_array = reader.ReadString();
                i++;
            }
        }
        reader.Close();
        //String[] Mp3SongLabel_array = (String[])
Mp3SongLabel_arraylist.ToArray(typeof(String));
        //return Mp3SongLabel_array;
        return my_array;
}
-------------------

And then I found another solution, beautiful to be injected directly in a
Flash DataSet, and then used the dataset objects freely everywhere around the
UI. The XML should be changed so to include attributes, as: <song label=""
data="" />.

And then you can create directly array of objects, bind them in Flash in a
DataSet and play around with them in arrays, objects, or whatever you like
format.

Could be of help to someone, as .NET webservice examples are really limited
(for now):

public class Song {
    public String label;
    public String data;
}

public class myWebService : System.Web.Services.WebService {

public myWebService() {
}

[WebMethod(Description = "Get the songs as objects with properties",
CacheDuration = 1)]
    public Song[] GetMp3Songs() {
        Song[] Songs_array = new Song[10];
        int i = 0;
        XmlTextReader reader = new XmlTextReader(Mp3Dir + "mp31.xml");
        reader.WhitespaceHandling = WhitespaceHandling.None;
        while (reader.Read()) {
            if (reader.Name == "Song") {
                Songs_array = new Song();
                Songs_array.label = reader.GetAttribute(0).ToString();
                Songs_array.data = reader.GetAttribute(1).ToString();
                i++;
            }
        }
        reader.Close();

        return Songs_array;
    }

}
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.