Posts Tagged ‘Flex 4.5’

Flex 4.5 and AmfPhp connection and Data transfer

November 16th, 2011 by aabhaanjan

1

Lets see the other way of data transfer between external environment and the Flex using AMFPHP. Important points regarding AmfPhp ?

1. AMF stands for Action Message Format.

2. It is a binary file that is used to represent a serialized ActionScript object.

3. It was designed to serialize and deserialize quickly.

4. AMF 3 for ActionScript 3 and AMF 0 is for (AS1 & AS2).

5. AMF 3 supports sending int and uint objects.

6. AMFPHP automatically converts ActionScript data types to PHP data types.

Lets have an example amfPhp and Flex 4.5 connection and data exchange between them.
Step 1 : Install AmfPhp in your wamp server :

amfphp_in_root_directory_www

Step 2 : If installed properly check the gateway.php . It should look like this :

amfphp_gateway_look

Step 3 : Check the browser . It should look like this :

amfphp_in_browser_look

Step 4 : Write a class for php. Here I am writting a dummy data as return which is containing Employee detail.

[xml]

fname = “Aava”;

$emp->lname = “Anjan”;

$emp->age = 28;

return $emp;

}

}

?>

[/xml]

One more class is refrencing the employee. Further we will expand it in our next example .

[xml]

[/xml]
Step 4 : Check the browser and Click on ‘call’ button you will get result like this :

result_on_call

Step 5 : Now Create a new Flex Project where we will see the employee detail by connecting
with AmfPhp . Here are the codes :

[xml]

<?xml version=”1.0″ encoding=”utf-8″?>
<s:Application xmlns:fx=”http://ns.adobe.com/mxml/2009″
xmlns:s=”library://ns.adobe.com/flex/spark”
xmlns:mx=”library://ns.adobe.com/flex/mx” minWidth=”955″ minHeight=”600″>
<fx:Declarations>
<!– Place non-visual elements (e.g., services, value objects) here –>
</fx:Declarations>
<fx:Script>
<![CDATA[
import com.RemotingServiceManager;
import flash.events.MouseEvent;
private var _rc:RemotingServiceManager = new RemotingServiceManager()
protected function search_btn_clickHandler(event:MouseEvent):void{
_rc.getEmployeeDetail();
}
]]>
</fx:Script>
<s:Button id=”search_btn” x=”158″ y=”141″ width=”189″ height=”34″ label=”Click to Get Employee Detail”
click=”search_btn_clickHandler(event)”/>
<s:Label x=”150″ y=”95″ color=”#570404″ fontSize=”17″ text=”Example of Flex and AmfPhp”/>
</s:Application>

[/xml]

Step 6 : Run and Click on the Search button . It will show you result :

flex_result

Tags: , , , , , , , , , , , , , , ,

Calling Flex Function From JavaScript

November 8th, 2011 by aabhaanjan
Here is an example :
Flex Side :
[xml]
<?xml version=”1.0″?>
<s:Application
xmlns:fx=”http://ns.adobe.com/mxml/2009″
xmlns:mx=”library://ns.adobe.com/flex/mx”
xmlns:s=”library://ns.adobe.com/flex/spark” creationComplete=”initApp()”>
<fx:Script>
import flash.external.*;
import mx.controls.Alert;
import flash.events.*;
private var alert:Alert;
public function initApp():void {
try {
ExternalInterface.addCallback(“myFlexFunction”,myFunc);
} catch(e:Error) {
trace(e)
}
alert = Alert.show(‘App is initialized ‘);
}
public function myFunc(msg:String):void {
alert = Alert.show(‘JavaScript is sending value to As \n ‘+msg);
}
</fx:Script>
</s:Application>
[/xml]

Here is an example :

Flex Side :

[xml]

<?xml version=”1.0″?>

<s:Application

xmlns:fx=”http://ns.adobe.com/mxml/2009″

xmlns:mx=”library://ns.adobe.com/flex/mx”

xmlns:s=”library://ns.adobe.com/flex/spark” creationComplete=”initApp()”>

<fx:Script>

import flash.external.*;

import mx.controls.Alert;

import flash.events.*;

private var alert:Alert;

public function initApp():void {

try {

ExternalInterface.addCallback(“myFlexFunction”,myFunc);

} catch(e:Error) {

trace(e)

}

alert = Alert.show(‘App is initialized ‘);

}

public function myFunc(msg:String):void {

alert = Alert.show(‘JavaScript is sending value to As \n ‘+msg);

}

</fx:Script>

</s:Application>

[/xml]

HTML Side :
[xml]
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” lang=”en” xml:lang=”en”>
<head>
<title>addCallback() Wrapper</title>
<script type=”text/javascript” src=”swfobject.js”></script>
<script type=”text/javascript”>
var swfVersionStr = “0”;
var xiSwfUrlStr = “”;
var flashvars = {};
var params = {};
params.quality = “high”;
params.bgcolor = “#ffffff”;
params.allowscriptaccess = “sameDomain”;
var attributes = {};
attributes.id = “ExternalInterfaceAddCallback”;
attributes.name = “ExternalInterfaceAddCallback”;
attributes.align = “middle”;
swfobject.embedSWF(
“ExternalInterfaceAddCallback.swf”, “flashContent”,
“100%”, “100%”,
swfVersionStr, xiSwfUrlStr,
flashvars, params, attributes);
</script>
</head>
<SCRIPT LANGUAGE=”JavaScript”>
function callApp() {
window.document.title = document.getElementById(“myName”).value;
var ExternalInterfaceAddCallback = document.getElementById(“ExternalInterfaceAddCallback”);
ExternalInterfaceAddCallback.myFlexFunction(window.document.title);
}
</SCRIPT>
<body>
<form id=”f1″>
Enter Your Name: <input type=”text” size=”30″ id=”myName” onchange=”callApp()”>
</form>
<div id=”flashContent”/>
</body>
</html>
[/xml]

Tags: , , , ,