有人可以告诉我在HTTP请求完成后如何检索HTTPService对象的引用(成功和错误)?这是一个存储HTTP请求的简单测试应用程序。处理响应时(" httpResult"和#34; httpFault"函数),似乎无法检索哪个调用成功/失败。
此应用输出始终"未找到呼叫"
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
initialize="init(event);">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
private var calls:Array;
private function init(event:FlexEvent):void{
calls = new Array();
// working HTTP Call
test('http://v4.ipv6-test.com/api/myip.php');
// not working HTTP Call
test('http://unknown.web.site.com/');
}
private function test(URL:String):void{
var service:HTTPService = calls[ calls.push(new HTTPService()) - 1];
service.url = URL;
service.method = 'GET';
service.addEventListener("result", httpResult);
service.addEventListener("fault", httpFault);
service.send();
}
private function httpResult(e:ResultEvent):void{
for(var i:int = calls.length; i>=0; i--){
if(calls[i]==e.target || calls[i]==e.currentTarget){
trace('Successful HTTP call found #' + i);
return;
}
}
trace('Successful HTTP call not found :(');
}
private function httpFault(e:FaultEvent):void{
for(var i:int = calls.length; i>=0; i--){
if(calls[i]==e.target || calls[i]==e.currentTarget){
trace('Unsuccessful HTTP call found #' + i);
return;
}
}
trace('Unsuccessful HTTP call not found :(');
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Placer ici les éléments non visuels (services et objets de valeur, par exemple). -->
</fx:Declarations>
</s:WindowedApplication>
答案 0 :(得分:0)
我发现了,感谢eeerahul的答案(HTTPService/ResultEvent with Flex 3.2 versus Flex >= 3.5)
看起来开发Flex的人想要在处理结果时隐藏我们的服务。无论如何,有一种方法可以通过访问私人(或受保护的)财产请求来检索服务的参考。事件对象。使用该属性,我们可以将结果与执行调用的服务相对应。
修复我问题的一行:
var curr_request:Object = AbstractOperation( e.currentTarget ).request;
新的工作代码:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
initialize="init(event);">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
import mx.rpc.events.*;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.AbstractOperation;
import mx.rpc.http.HTTPService;
private var calls:Array;
private function init(event:FlexEvent):void{
calls = new Array();
// working HTTP Call
test('http://v4.ipv6-test.com/api/myip.php');
// not working HTTP Call
test('http://unknown.web.site.com/');
}
private function test(URL:String):void{
var service:HTTPService = calls[ calls.push(new HTTPService()) - 1];
service.url = URL;
service.method = 'GET';
service.addEventListener("result", httpResult);
service.addEventListener("fault", httpFault);
service.send();
}
private function httpResult(e:ResultEvent):void{
var curr_request:Object = AbstractOperation( e.currentTarget ).request;
for(var i:int = calls.length-1; i>=0; i--){
if(calls[i].request==curr_request){
calls[i].removeEventListener("result", httpResult);
calls[i].removeEventListener("fault", httpResult);
trace('Successful HTTP call found #' + i);
return;
}
}
trace('Successful HTTP call not found :(');
}
private function httpFault(e:FaultEvent):void{
var curr_request:Object = AbstractOperation( e.currentTarget ).request;
for(var i:int = calls.length-1; i>=0; i--){
if(calls[i].request==curr_request){
calls[i].removeEventListener("result", httpResult);
calls[i].removeEventListener("fault", httpResult);
trace('Unsuccessful HTTP call found #' + i);
return;
}
}
trace('Unsuccessful HTTP call not found :(');
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Placer ici les éléments non visuels (services et objets de valeur, par exemple). -->
</fx:Declarations>
</s:WindowedApplication>