PDA

View Full Version : XML bug?



Huinoio
May 18th, 2007, 03:49 PM
I use flash to communicate with server side script with XML and it all quit working with AS3.0. It used to work great with the XMLsendandload from AS2.0 and I have been banging my head against the wall and talking to tech support, but they have assured me it works and I'm doing something wrong. Turns out that URLloader/request doesnt' quite work as advertised and it is incapable of sending an XML packet to the server, but treats the XML packet like a big variable and adds "data=" to the beginning of the packet, which makes it invalid to anything receiving it. Seems like a bummer that the new 3.0 touts such a great XML advancement yet fails at such a primary function. I hate having to change all the server side script since any other software using the script works fine. Maybe I have to make a duplicate script for all of them and name them slightly different from the "real" ones. I'm thinking of a lot of great naming variations now after spending all this time.

smizzle
May 18th, 2007, 04:08 PM
did you use the URLVariables class? I have to do something similar and I was able to set the URLRequest.data equal to my URLVariables object and the add a URLLoader object and it works fine. Make sure you set the URLRequest method to post as well.

senocular
May 18th, 2007, 04:08 PM
Ive had no problems. Share your code.

Huinoio
May 21st, 2007, 06:55 PM
OK, sorry I took so long to respond, so here's the source:

package {
import flash.display.*;
import flash.events.*;
import flash.net.*;
public class xmlcom extends Sprite {

public function xmlcom() {
handleSave();
}
private function handleSave():void {
// Create a new XML instance containing the data to be saved
var dataToSave:XML=<Point><PointID>913</PointID></Point>;
// Point the request to the script that will handle the XML
var request:URLRequest=new URLRequest("http://localhost:1450/bajci/testcom.aspx");
// Set the data property to the dataToSave XML instance to send the XML
// data to the server
request.data=dataToSave;
// Set the contentType to signal XML data being sent
request.contentType="text/xml";
// Use the post method to send the data
request.method=URLRequestMethod.POST;
// Create a URLLoader to handle sending and loading of the XML data
var loader:URLLoader=new URLLoader;
// When the server response is finished downloading, invoke handleResponse
loader.addEventListener(Event.COMPLETE,handleRespo nse);
// Finally, send off the XML data to the URL
loader.load(request);
}
private function handleResponse(event:Event):void {
try {
// Attempt to convert the server's response into XML
var success:XML=new XML(event.target.data);
trace(success);
} catch (e:TypeError) {
// Display an error message since the server response was not understood
trace("Could not parse XML response from server.");
}
}
}
}



and here's the server side script in .net:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
// added
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Xml;
public partial class testcom : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string PointID = "0";
string xString = Server.UrlDecode(Request.Form[0]);
System.IO.StringReader sr = new System.IO.StringReader(xString);
XmlTextReader xmlReader = new XmlTextReader(sr);
while (xmlReader.Read())
{
if (xmlReader.IsStartElement())
{
switch (xmlReader.Name)
{
case "PointID":
PointID = xmlReader.ReadString();
break;
// other case statements here
}
}
}
// start creating xml response
XmlTextWriter xmlWriter = new XmlTextWriter(Response.OutputStream, System.Text.Encoding.UTF8);
// start xml document with header attributes
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("Pointsummary");
xmlWriter.WriteAttributeString("PointID", PointID);
xmlWriter.WriteStartElement("SuccessCode");
xmlWriter.WriteString("1");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Close();

}
}


and here's the error flash throws:
Error opening URL 'http://localhost:1450/bajci/testcom.aspx'
Error #2044: Unhandled ioError:. text=Error #2032: Stream Error. URL: http://localhost:1450/bajci/testcom.aspx
at xmlcom/::handleSave()
at xmlcom$iinit()


and here's the same thing only remove the server side script part that is looking for a response in xml:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
// added
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Xml;
public partial class testcom : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string PointID = "913";

// start creating xml response
XmlTextWriter xmlWriter = new XmlTextWriter(Response.OutputStream, System.Text.Encoding.UTF8);
// start xml document with header attributes
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("Pointsummary");
xmlWriter.WriteAttributeString("PointID", PointID);
xmlWriter.WriteStartElement("SuccessCode");
xmlWriter.WriteString("1");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Close();

}
}

and flash returns and traces the little xml doc perfectly so the script is communicating back to flash great with no errors.
And here's Adobe's response concerning the problem:


Web Case Details: [172625515]

__________________________________________________ ___________
______________________________________________
*** Notes to Customer ***
05/16/2007 22:13:00
______________________________________________
Hi Skip,
Thank you for your consistent reply.
I actually consulted my senior already regarding this. If you really
wish to use the URLRequest class then you would have to do some
workaround on the server side how to
truncate ore remove the "data=" part of the passed value. If you are
using php, you need to create a php function that would eliminate this
part for you to get the exact tags.
This is what you can do as of now. If you think this feature is really
needed you may post a feature request on our site:
http://www.adobe.com/go/wish
I hope this information has helped you.
Thanks and have a nice day.
Regards,
Christine R.
Technical Support Engineer
Adobe Systems Inc.
______________________________________________
*** Notes from Customer ***

Huinoio
May 21st, 2007, 07:02 PM
In addition, I found this on the Adobe site where another programmer discusses the same exact problem....

http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?forumid=72&catid=612&threadid=1250216&enterthread=y

I hope there is something I'm not getting right with this problem.
Thanks

Huinoio
May 26th, 2007, 07:04 PM
Any ideas?

Huinoio
May 29th, 2007, 12:56 AM
OK, I finally got it working. I really don't know why, but I want to share my code for anyone else who might have this problem. Thanks to the replies to keep me egged on. Those that indicated it should work or did work are the only thing that kept me going, because Adobe's response and the link I pointed to above had me discouraged. Anyway, here's what I know and I appologize if some look at this and think "duhh".

This is the .as file:

package {
import flash.display.*;
import flash.events.*;
import flash.net.*;
public class xmlcom extends Sprite {

public function xmlcom() {
handleSave();
}
private function handleSave():void {
// create an XML object
var dataToSave:XML=<Point><PointID>91344</PointID></Point>;

// Point the request to the script that will handle the XML
var request:URLRequest=new URLRequest("http://localhost/whatnot.aspx");

// Set the data property to the dataToSave XML instance to send the XML
// data to the server
request.data=dataToSave;

// Use the post method to send the data
request.method=URLRequestMethod.POST;

// Create a URLLoader to handle sending and loading of the XML data
var loader:URLLoader=new URLLoader;

// When the server response is finished downloading, invoke handleResponse
loader.addEventListener(Event.COMPLETE,handleRespo nse);
// Finally, send off the XML data to the URL
loader.load(request);
}
private function handleResponse(event:Event):void {
try {
// Attempt to convert the server's response into XML
var success:XML=new XML(event.target.data);
trace(success);
} catch (e:TypeError) {
// Display an error message since the server response was not understood
trace("Could not parse XML response from server.");
}
}
}
}


Since I use .net for server side work, here it is:

protected void Page_Load(object sender, EventArgs e)
{
string PointID = "0";

string xString = Server.UrlDecode(Request.Form[0]);
System.IO.StringReader sr = new System.IO.StringReader(xString);
XmlTextReader xmlReader = new XmlTextReader(sr);
while (xmlReader.Read()){
switch (xmlReader.Name)
{
case "PointID":
PointID = xmlReader.ReadString();
break;
// other case statements here
}
}
// start creating xml response
XmlTextWriter xmlWriter = new XmlTextWriter(Response.OutputStream, System.Text.Encoding.UTF8);
// start xml documnt with header attributes
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("Pointsummary");
xmlWriter.WriteAttributeString("PointID", PointID);
xmlWriter.WriteStartElement("PointValue");
xmlWriter.WriteString("12");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Close();
}

Most importantly, lead the .aspx page with this:

<%@ Page Language="C#" ContentType="text/xml" ValidateRequest="false" ....etc... etc..

I appologize for doubting the product and haven't had any other problems (to speak of) over the years. Just the new stuff and all the changes are making me jumpy.