Parse Error When Trying to Set a Break in Flex 3 - apache-flex

I have been trying unsuccessfully to set a HTTP break in my Flex 3 project. Obviously, I am totally clueless about programming and I don't have many references. When I try to export the project I receive parse errors for the result handler and var fault string. I am attaching a code snippet of where I have been placing the break.
<mx:HTTPService id="getData" url="http://www.myurl.com"
useProxy="false" method="GET" resultFormat="text" resultType="text"
result="resultHandler(event)" fault="faultHandler(event)">
private function resultHandler(e:ResultEvent):void {
trace(e.result);
}
private function resultHandler(e:FaultEvent):void {
var faultstring:String = event.fault.faultString;
Alert.show(faultstring);
}
<mx:request xmlns="">
<getTutorials>"true"</getTutorials>
</mx:request>
I think this may have to do with the PHP file and the type of data the Flex is looking for? Here is the first bit of the error I am receiving in the browser.
TypeError: Error #1034: Type Coercion failed: cannot convert "[{"id":"2","name":"Strapless Wedding Dress Tips","author":"Ramona Waters","rating":"0"},{"id":"3","name":"Coordinating Your Brides Maids","author":"Ericka Brown","rating":"0"}]" to mx.controls.Alert.
at DressBuilder2/resultHandler()
at DressBuilder2/__getData_result()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()

Update: Cool, you've got your code compiled! Try with the following:
Set resultFormat=array if you have an array of objects. Get this value in an array and loop over to see if you have can see the items. If this doesn't work try next tip.
Remove the resultFormat from the HTTPService tag (which is the same as if you had set it to object). See this.
The functions in AS typically go inside a <mx:Script> tag. That's the first thing to fix. You will also have to import the definitions of the classes you are using. Have a look here:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" width="535" height="345"
creationComplete="getData.send()">
<mx:Script>
<![CDATA[
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
import mx.rpc.http.HTTPService;
private function resultHandler(e:ResultEvent):void {
Alert(e.result.toString());
}
private function faultHandler(e:FaultEvent):void {
Alert(e.fault.toString());
}
]]>
</mx:Script>
<mx:HTTPService id="getData" resultFormat="text"
fault="faultHandler(event)" result="resultHandler(event)"
url="http://www.myurl.com"/>
</mx:Application>
Try using this MXML file and let us know how far you got.

Related

Flex 3: HTTPService not working for one file... anybody see anything wrong with the code?

So, i've got three HTTPService calls for three different XML files:
<mx:HTTPService id="projectsHttp" url="projects.xml" resultFormat="e4x" makeObjectsBindable="true" result="countProjects(event)" />
<mx:HTTPService id="studentsHttp" url="students.xml" resultFormat="e4x" makeObjectsBindable="true" result="createStudentsCollection(event)" />
<mx:HTTPService id="dprepHttp" url="directorsPrep.xml" resultFormat="e4x" makeObjectsBindable="true" result="createPhase(event)" />
The first two work great... but that last one just won't work. For testing purpose, the createPhase function looks like this:
public function createPhase(e:ResultEvent):void
{
Alert.show("Testing");
}
Also, the directorsPrep.xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<directorspreps>
<directorsprep>
<prepid>1</prepid>
<title>dir. prep. 1</title>
<workingtitle>dp1 WT</workingtitle>
<startdate>7/7/2011</startdate>
<numdays>2</numdays>
<positions>
<role>1D</role>
<role>2D</role>
<role>1C</role>
</positions>
</directorsprep>
<directorsprep>
<prepid>2</prepid>
<title>dir. prep. 2</title>
<workingtitle>dp2 WT</workingtitle>
<startdate>7/10/2011</startdate>
<numdays>3</numdays>
<positions>
<role>1D</role>
<role>2D</role>
<role>1C</role>
<role>GE</role>
</positions>
</directorsprep>
</directorspreps>
Anybody see anything that would prevent the directorsPrep.xml file from not loading?
EDIT:
I'm a moron... Didn't do the .send(); :( Sorry for the time waster
It is tough to say for sure. I created a little project in Flex3 that includes your XML file and it worked fine for me. You should add a fault handler to know why it is failing. Put a breakpoint in that handler if you need to examine things. Also, make sure that you are calling send() in order for that XML file to get loaded. Here is an example of what was working for me (including the fault handler).
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
public function createPhase(e:ResultEvent):void
{
Alert.show(e.result.toString());
}
protected function createPhaseFailed(e:FaultEvent):void
{
Alert.show(e.message.toString());
}
]]>
</mx:Script>
<mx:HTTPService id="dprepHttp" url="directorsPrep.xml" resultFormat="e4x" makeObjectsBindable="true"
result="createPhase(event)" fault="createPhaseFailed(event)" />
<mx:initialize>
<![CDATA[
dprepHttp.send();
]]>
</mx:initialize>
</mx:Application>
Good luck!

calling function from actionscript (.as) file for button in mxml

Can anyone help me in this regard?
I have Actinscript file in which I have defined a function like below:
actionScript.as (file name)
import mx.controls.Alert;
public function abc():void{
Alert.show("Inside abc(): My Button Clicked");
}
Now I have a button in mxml and I am calling the above function in my buttion "click" attribute like below.
Importing script in mxml:
<mx:Script source="actionScript.as" />
Using function:
<mx:Button id="button1" label="My Button" click="abc()"/>
Can any one help me? Is there anything else I need to do or am I going wrong somewhere?
create a new project and make these 2 files
test.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script source="includes/test.as"/>
<mx:Button label="Alert Test" click="abc()" />
</mx:Application>
src/includes/test.as
// ActionScript file
import mx.controls.Alert;
public function abc():void{
Alert.show("Inside abc(): My Button Clicked");
}
works fine for me
wrap up your .as file in a package and class reference. instantiate the class in your MXML and call the function using instantiated class.
var Class1:Something = new Something();
Class1.abc();

Why is my object reference null when trying to reference an mx:Canvas from ActionScript?

this is a follow up question from this one, I don't want to keep going in the comments and preventing people from getting hard-earned reputation... :)
In my Cairngorm command class, to get it to compile I needed to tell it what myCanvas was, so I used this line:
var myCanvas : MyCanvas = new MyCanvas;
I'm guessing that's wrong, though, because although it compiles, if I try to do something like this:
if (myCanvas.subObject.value == 0) { ... }
it'll throw this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at com.foo.bar.command::MyCommand/execute()
as if the subObject doesn't exist. It looks like I might be getting a new instance of MyCanvas, not the instance I want from the main.mxml with an id of myCanvas. Am I right? How do I fix this?
Edit (10:59pm GMT+1): Okay, so it looks like I've been way too vague here. Here's my main.mxml:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:control="com.foo.bar.control.*"
xmlns:business="com.foo.bar.business.*"
xmlns:view="com.foo.bar.view.*"
applicationComplete="com.foo.bar.util.StartupUtil.init()"
horizontalScrollPolicy="off"
verticalScrollPolicy="off"
borderThickness="0"
paddingBottom="0"
paddingLeft="0"
paddingTop="0"
paddingRight="0"
>
<mx:Script>
<![CDATA[
import com.foo.bar.model.PlayerModelLocator;
[Bindable]
private var model : PlayerModelLocator = PlayerModelLocator.getInstance();
]]>
</mx:Script>
<!-- ========================================================================== -->
<!-- the ServiceLocator where we specify the remote services -->
<business:Services id="services" />
<!-- the FrontController, containing Commands specific to this application -->
<control:PlayerController id="controller" />
<!-- ========================================================================== -->
<mx:Style source="assets/main.css" />
<view:MyCanvas id="myCanvas" />
</mx:Application>
And here's my com/foo/bar/command/MyCommand.as:
package com.foo.bar.command {
/* add to controller
addCommand( MyEvent.EVENT_CHANGE_VOLUME, ChangeVolumeCommand );
*/
import flash.net.SharedObject;
import com.adobe.cairngorm.control.CairngormEvent;
import com.adobe.cairngorm.commands.ICommand;
import com.foo.bar.model.PlayerModelLocator;
import com.foo.bar.event.MyEvent;
import com.foo.bar.view.*;
public class ChangeVolumeCommand implements ICommand {
public function execute(event:CairngormEvent):void {
var model : PlayerModelLocator = PlayerModelLocator.getInstance();
var myEvent : MyEvent = MyEvent(event);
var myCanvas : MyCanvas = new MyCanvas();
var so:SharedObject = SharedObject.getLocal("fixie.video");
if (myCanvas.subObject.value == 0) {
trace("subobject value is 0");
}
}
}
}
Basically, I want to get a handle on the object with ID myCanvas in main.mxml using the myCanvas object in MyCommand.as
Could be a couple of things. First, you need parentheses on your class name after the "new" statement: new MyCanvas(); Second, you may be trying to access your sub component before the component lifecycle is ready for you to do so. (It's hard to tell from the code you posted since there isn't enough context.)
What is the scope of your myCanvas variable? Is it inside a method somewhere? You will need to make it public or give it getter/setter to be able to access it.
You may also be trying to reference it before it has been added to its parent, using the addChild() method.
There really isn't enough code in your examples to determine the problem, but these things should give you somewhere to start looking.
1 way is to add eventListener when your myCanvas will be ready after CreationComplete and to do all your stuff
and the second is:
define your subObject as in myCanvas class so you'll be able to access it on Init Stage of your component.
regards
Eugene
p.s. all of the time everybody want to get answer without well formed sample of their problem, its terrible!!

Why wouldn't a flex remoteobject be able to work within a custom component?

Please enlighten this flex noob. I have a remoteobject within my main.mxml. I can call a function on the service from an init() function on my main.mxml, and my java debugger triggers a breakpoint. When I move the remoteobject declaration and function call into a custom component (that is declared within main.mxml), the remote function on java-side no longer gets called, no breakpoints triggered, no errors, silence.
How could this be? No spelling errors, or anything like that. What can I do to figure it out?
mxml code:
&#060 mx:RemoteObject id="myService"
destination="remoteService"
endpoint="${Application.application.home}/messagebroker/amf" &#062
&#060 /mx:RemoteObject &#062
function call is just 'myService.getlist();'
when I move it to a custom component, I import mx.core.Application; so the compiler doesn't yell
my child component: child.mxml
<mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()" >
<mx:Script>
<![CDATA[
import mx.core.Application;
public function init():void {
helloWorld.sayHello();
}
]]>
</mx:Script>
<mx:RemoteObject id="helloWorld" destination="helloService" endpoint="$(Application.application.home}/messagebroker/amf" />
<mx:Label text="{helloWorld.sayHello.lastResult}" />
</mx:Panel>
my main.mxml:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()" xmlns:test="main.flex.*" >
<mx:Script>
<![CDATA[
[Bindable]
public var home:String;
[Bindable]
public var uName:String;
public function init():void {
//passed in by wrapper html
home = Application.application.parameters.appHome;
uName = Application.application.parameters.uName;
}
]]>
</mx:Script>
<test:child />
</mx:Application>
The child components are calling creationComplete before the parent (so home is null). A solution is to throw an event (like InitDataCompleted) from the parent after you read the data, and in the child components listen for this event (so don't rely on creationcomplete in the child).
However more important than that is how can you diagnose in future this kind of problems. A simple tool like a proxy (eg Charles) should help.
For your endpoint value you've got
endpoint="$(Application.application.home}/messagebroker/amf"
Why are you using $( before Application.application... This should be a { as in:
endpoint="{Application.application.home}/messagebroker/amf"

flex builder: how to populate an array from an external file of strings

hello i'm new to flex builder and trying to populate an array from an external file consisting of a list of strings.
how do i go about that? should i use some sort of a data object?
Here's an example to get you started:
Sample File (file_with_strings.txt):
one, two, three
Sample App
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
initialize="initializeHandler()">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
protected function initializeHandler():void
{
service.send();
}
protected function updateList(result:Object):void
{
var array:Array = result.split(/,\s+/);
var collection:ArrayCollection = new ArrayCollection(array);
list.dataProvider = collection;
}
]]>
</mx:Script>
<mx:HTTPService id="service"
url="file_with_strings.txt"
resultFormat="text" result="updateList(event.result)"/>
<mx:List id="list"/>
</mx:Application>
I would just use the HTTPService class to load your external file. You can change the resultFormat to XML, Object, and a few other things if you'd like. Then just customize that updateList() method however.
Hope that helps,
Lance

Resources