Flash object cannot connect to remote server - actionscript-3

My company design digital signage solutions for companies. The animated widgets are typically produced in Flash.
In a current project I'm experiencing a bit of an odd issue. The screen is split into 3 areas: Video, RSS Ticker, and Weather. The RSS and Weather objects are created using Flash. Each object is it's own Flash file.
Both Flash object utilize the same method to connect to the remote servers:
function loadWeatherFromUrl():void
{
var urlRequest:URLRequest = new URLRequest("MY URL");
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, completeHandler);
try{
urlLoader.load(urlRequest);
} catch (error:Error) {
trace("Cannot load : " + error.message);
}
}
function completeHandler(event:Event):void {
var loader:URLLoader = URLLoader(event.target);
//Do stuff with the data...
}
When testing in the IDE, both the Weather object and the RSS object work fine, as seen below:
Once deployed to the signage software it doesn't work anymore. However, the Weather object works as expected. The Flash object doesn't encounter a URL loading error either. Compare pic #2 and pic #3 to see how I view errors.
If I try to preview both the Weather object and the RSS object locally in a web-browser (IE, Firefox), I do get local permission errors, as expected.
So any ideas why one would work and the other wouldn't? If it had something to do with CORS (Cross Origin Resource Sharing) wouldn't both fail? I have tried both options (local only / network only) in the Publish -> Playback settings.
The weather object is powered by Yahoo:
Yahoo Weather
and the RSS object is powered by CTV News (Canadian Television Network):
CTV RSS Feed
EDIT:
For the RSS feed (above I used the Weather object's methods, they're identical except the function name and error output) I have the following:
At run time
error_msg.text = "Loading...";
Then the URL loading:
function loadFeedFromUrl():void
{
var urlRequest:URLRequest = new URLRequest("http://ottawa.ctvnews.ca/rss/ctv-news-ottawa-1.1245493");
error_msg.text = "";
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, completeHandler);
try{
urlLoader.load(urlRequest);
} catch (error:Error) {
trace("Cannot load : " + error.message);
error_msg.text = error.message;
}
}
EDIT #2
Here is the UPDATED loading function, as well as the basic error handling functions.
function loadFeedFromUrl():void
{
var urlRequest:URLRequest = new URLRequest("http://ottawa.ctvnews.ca/rss/ctv-news-ottawa-1.1245493");
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, completeHandler);
try{
urlLoader.load(urlRequest);
} catch (error:Error) {
trace("Cannot load : " + error.message);
error_msg.text = error.message;
}
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandlerFunction);
urlLoader.addEventListener(IOErrorEvent.NETWORK_ERROR, ioErrorHandlerFunction);
urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandlerFunction);
}
function ioErrorHandlerFunction() {
error_msg.text = "IO Error!";
}
function securityErrorHandlerFunction() {
error_msg.text = "Security Error!";
}
The error_msg textfield stays "Loading...", so it doesn't appear any of those error types are being received.

Related

How to handle error dialog boxes in ActionScripts 3

I write some code to check an XML from a URL, it works when Internet connection exist, but when i manually disable internet for test an Error dialog box show, i coudn't handle it with try() catch().
try {
var myXML: XML = new XML();
var XML_URL: String = "https://drive.google.com/uc?export=download&id=0B5pkl4TJ7V0OTFBPanUyMWQxUnM";
var myXMLURL: URLRequest = new URLRequest(XML_URL);
var myLoader: URLLoader = new URLLoader(myXMLURL);
myLoader.addEventListener(Event.COMPLETE, xmlLoaded);
function xmlLoaded(event: Event): void {
myXML = XML(myLoader.data);
trace("LOCK CODE: " + myXML.LOCK);
if (myXML.LOCK == 0) {
gotoAndStop(71);
};
}
} catch (e: Error) {
gotoAndStop(71);
trace("FAILED TO CHECK LOCK.");
} finally {
gotoAndStop(71);
trace("FAILED TO CHECK LOCK.");
}
How can i hide this dialog from user?
Add an URLLoader event listener for IOErrorEvent.IO_ERROR
e.g.
myLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
function ioErrorHandler(event:IOErrorEvent):void {
trace("ioErrorHandler: " + event);
}
You should handle all events as good measure anyway (e.g. security error, etc.)
The as3 docs example provides a helpful snippet
Ideally you would not only silently trace message, but hopefully display helpful feedback or placeholder content for the user to understand what to expect.

AS3 Flash URLRequest not working upon export

This simple login form works within the test environment of CS6 (Flash 11.4) but upon exporting does not work. I've narrowed down the issue to the actual URLRequest not working properly. Hopefully somebody can shed some light!
Many thanks, Nick :)
AS3
login.loginSubmit.addEventListener(MouseEvent.CLICK, function(){
if(login.loginPassword.text!="Password" && login.loginPassword.text!=""){
login.loginSubmit.enabled = false;
// Begin URL setup for login
var loginVariables:URLVariables = new URLVariables("email="+login.loginEmail.text+"&password="+login.loginPassword.text);
var loginRequest:URLRequest = new URLRequest();
loginRequest.url = "login.php";
loginRequest.method = URLRequestMethod.POST;
loginRequest.data = loginVariables;
var loginLoader:URLLoader = new URLLoader();
loginLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
loginLoader.addEventListener(Event.COMPLETE, loginHandler);
function loginHandler(event:Event):void {
if(loginLoader.data.passed=="true"){
var hideInitial:Tween = new Tween(uiInitial, "x", Strong.easeOut, 6, -315, 0.5, true);
hideInitial.addEventListener(TweenEvent.MOTION_FINISH, function(){
member.data.email = loginLoader.data.email;
member.data.fname = loginLoader.data.fname;
member.data.lname = loginLoader.data.lname;
member.flush();
});
}else{
trace("error");
}
}
// Send PHP/SQL request
loginLoader.load(loginRequest);
}
});
You need to enable the network access for your published SWF. Go to Publish Settings for Flash (.swf) and Set the "Local playback security" settings to "Access Network Only".
Turns out that there is both a local and external URLRequest, causing trouble with the settings. I've made both the same (external) and of course hosted them on the same domain as the Flash, now works.

URLrequest cache response not working

I want to cache the response of a urlRequest for offline usage in Adobe Air. When I compile for flash player the cache works and I get the response even when I disconnect the network, but when I compile for Adobe Air I get error. PS: useCache and cacheResponse dose not work!
stage.addEventListener(MouseEvent.CLICK , callReq)
var loader:URLLoader = new URLLoader()
function callReq(e:Event):void
{
//URLRequestDefaults.manageCookies = true;
//URLRequestDefaults.useCache = true;
var r:String = "http://onecom.no/presentation_json.php?what=get_slides&slide_id[]=2540"
var urlRequest:URLRequest = new URLRequest(r)
// urlRequest.cacheResponse = true
// urlRequest.useCache = true
urlRequest.url = r
loader.addEventListener(Event.COMPLETE , Comp)
loader.load(request)
}
function Comp(e:Event):void
{
trace( e.target.data)
}
Air can not use the browsers cache. I had to build my own cache class that saves all URLRequest responses to a Shared Object and loads them if there is no internet connection
save them to variables and store them in the local SQLlite database on your device/computer?...
Using local SQLlite on device

FileReference: post file without browsing

I am trying to upload a file to the server. I'm doing it like this:
var fileRef:FileReference = new FileReference();
fileRef.addEventListener(flash.events.Event.SELECT, selectHandler);
fileRef.addEventListener(flash.events.Event.COMPLETE, completeHandler);
fileRef.addEventListener(ProgressEvent.PROGRESS, normalprogressHandler);
fileRef.browse();
function selectHandler(event:flash.events.Event):void
{
var params:URLVariables = new URLVariables();
params.date = new Date();
params.ssid = "94103-1394-2345";
var request:URLRequest = new URLRequest("http://www.test.com/Uploads");
request.method = URLRequestMethod.POST;
request.data = params;
fileRef.upload(request, "Custom1");
}
function completeHandler(event:flash.events.Event):void
{
trace("uploaded");
}
function normalprogressHandler(event:ProgressEvent):void
{
var percent:Number = Math.floor((event.bytesLoaded * 100)/ event.bytesTotal );
trace(percent+"%");
}
Would it be possible to upload a file but without browsig for it? I want to decide myself what file to upload instead of the user performing a browse first
You can't do that with FileReference which has the following limitations (reference):
The load() and save() APIs can only be called in response to user
interaction (such as a button click).
The locations of the loaded and save files are not exposed to
ActionScript.
The APIs are asynchronous (non-blocking).
Clearly, it would represent a major security risk if the Flash player was arbitrarily allowed to upload anything from your local file system to a remote server.
If you're trying to upload from an AIR app, you can do what you're trying to do with the File class.

Actionscript 3 Sound ioError Problem

i am having trouble with sound. i am loading it dynamically, url comes from flashvars.
App is working actually but stil gives error, unhandled ioError. but i already handled it.
`
var sound:Sound = new Sound()
try{
sound.load(new URLRequest(req));
} catch(e:IOError){
trace("catch ioerror");
}
sound.addEventListener(IOErrorEvent.IO_ERROR, function(evt:IOErrorEvent):void { trace("error:",evt) } );
sound.addEventListener(Event.COMPLETE, function(e:Event):void{
channel = sound.play(0,int.MAX_VALUE);
});`
Just to rule it out, try commenting out the line channel = sound.play(0,int.MAX_VALUE); perhaps it is this that is throwing the error and not the load, and you have no catch around this.
Try a neater approach in terms of code, might just have issues with your layout and such:
var request:URLRequest = new URLRequest(req);
sound.load(request);
sound.addEventListener(IOErrorEvent.IO_ERROR, _ioError);
sound.addEventListener(Event.COMPLETE, _complete);
function _ioError(e:IOErrorEvent):void
{
trace("File was not found");
_removeListeners();
}
function _complete(e:Event):void
{
channel = sound.play(0,int.MAX_VALUE);
_removeListeners();
}
function _removeListeners():void
{
sound.removeEventListener(IOErrorEvent.IO_ERROR, _ioError);
sound.removeEventListener(Event.COMPLETE, _complete);
}