all groups > flash data integration > august 2005 >
You're in the

flash data integration

group:

Binding .NET WebService methods to Actionscript arrays


Binding .NET WebService methods to Actionscript arrays Dimitar
8/25/2005 12:00:00 AM
flash data integration:
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
Re: Binding .NET WebService methods to Actionscript arrays LBStorm
8/25/2005 6:39:40 PM
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


Re: Binding .NET WebService methods to Actionscript arrays Dimitar
8/25/2005 11:07:16 PM
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;
}

}
AddThis Social Bookmark Button