我有一个URLStream对象,它接受XML格式的响应,但不幸的是,响应的前4个字符被切断,使得xml格式不正确。
如果我使用URLLoader对象,则从服务器返回的xml格式正确。
有人知道如何解决这个问题吗?
private var _stream:URLStream;
_myVariables = new URLVariables();
_myVariables.email = _un;
_urlRqSend = new URLRequest(_loginURL);
var encoder:Base64Encoder = new Base64Encoder();
encoder.encode("password:"+_pw);
var credsHeader:URLRequestHeader = new URLRequestHeader("Authorization", "Basic " + encoder.toString());
_urlRqSend.requestHeaders.push(credsHeader);
_urlRqSend.data = _myVariables;
_urlRqSend.method = URLRequestMethod.POST;
_stream = new URLStream();
_stream.addEventListener(flash.events.Event.COMPLETE, handleResponse);
_stream.load(_urlRqSend);
private function handleResponse(ev:flash.events.Event):void{
trace("returned data: ",_stream.readUTFBytes(_stream.bytesAvailable));
}
输出
//返回数据:l version="1.0"...etc
正确的回答应该是
//返回数据:<?xml version="1.0"...etc
由于
答案 0 :(得分:0)
这是一个简单的解决方法:
var xml:String = _stream.readUTFBytes(_stream.bytesAvailable)
if(xml.search("<?xml version="1.0" != 0)){ //you may have to mix the "-s to compile.
var start:int=xml.search("?>") + 3 //this will give you the character of the first element after the xml declaration (like <root> ...)
xml = '<?xml version="1.0"...etc' + xml.Splice(start); //splice will return <root> ..., and we'll add the declaration before it.
}
我将错误编码的swome转移到哪里,请使用跟踪/调试来工作,但接近这一点可以解决问题。您应该在市长浏览器中查看它,以确保它在任何地方都能正常工作。