PDA

View Full Version : OpenAMF Flash Remoting Assistance?



dewey
July 5th, 2006, 08:34 AM
http://www.flash-db.com/Tutorials/helloOpenamf/hello2.php

That, my friends, is all that I had to work with to learn Flash Remoting with OpenAMF.

All I am trying to do at the moment is send a custom object, known as a "SmackletType" from Java to Flash. I have basically just edited what I had in that tutorial (which was working fine) in order to pass this SmackletType object across. I am so close to getting this that I can taste it, but I just need a little bit of help with the end here...

Perhaps some code will help...

This is my SmackletType objet in Java:


package com.flashdb.domain;

import java.io.Serializable;

public class SmackletType implements Serializable {

// class properties
private String _smackletName;
private String _smackletTeaseLink;
private String _smackletLink;
private String _smackletThumbnail;

// constructor

public SmackletType(String smackletName, String smackletLink, String smackletTeaseLink, String smackletThumbnail) {

_smackletThumbnail = smackletThumbnail;
_smackletTeaseLink = smackletTeaseLink;
_smackletLink = smackletLink;
_smackletName = smackletName;

} // SmackletType constructor

// getters and setters

public String get_smackletLink() {
return _smackletLink;
}

public void set_smackletLink(String link) {
_smackletLink = link;
}

public String get_smackletName() {
return _smackletName;
}

public void set_smackletName(String name) {
_smackletName = name;
}

public String get_smackletTeaseLink() {
return _smackletTeaseLink;
}

public void set_smackletTeaseLink(String teaseLink) {
_smackletTeaseLink = teaseLink;
}

public String get_smackletThumbnail() {
return _smackletThumbnail;
}

public void set_smackletThumbnail(String thumbnail) {
_smackletThumbnail = thumbnail;
}

} // SmackletType class


My SmackletType object in ActionScript:


class SmackletType {

// class properties
private var _smackletName:String;
private var _smackletTeaseLink:String;
private var _smackletLink:String;
private var _smackletThumbnail:String;

// constructor

public function SmackletType(smackletName:String, smackletLink:String, smackletTeaseLink:String, smackletThumb:String) {

_smackletThumbnail = smackletThumb;
_smackletTeaseLink = smackletTeaseLink;
_smackletLink = smackletLink;
_smackletName = smackletName;

} // SmackletType constructor

// getters and setters
public function get smackletName():String {
return _smackletName;
}

public function set smackletName(sSmackletName:String):Void {
_smackletName = sSmackletName;
}

public function get smackletLink():String {
return _smackletLink;
}

public function set smackletLink(sSmackletLink:String):Void {
_smackletLink = sSmackletLink;
}

public function set smackletTeaseLink(sSmackletTeaseLink:String):Void {
_smackletTeaseLink = sSmackletTeaseLink;
}

public function get smackletTeaseLink():String {
return _smackletTeaseLink;
}

public function get smackletThumbnail():String {
return _smackletThumbnail;
}

public function set smackletThumbnail(sSmackletThumbnail:String):Void {
_smackletThumbnail = sSmackletThumbnail;
}

} // SmackletType class


This is my openamf-config.xml file custom mapping for the SmackletType object. I have not added anything else to the .xml file:


<custom-class-mapping>
<java-class>com.flashdb.domain.SmackletType</java-class>
<custom-class>SmackletType</custom-class>
</custom-class-mapping>


This is the Java file that "holds" the method that will be called by Flash. It is just a simple edit of the code that was outlined in the tutorial, but I am returning a SmackletType instead of a String:


package com.flashdb.services;

import com.flashdb.domain.*;

public class HelloWorld {

SmackletType testSmackletType = new SmackletType("Slots", "http://www.google.com", "http://www.google.ca", "00.gif");

public SmackletType makeEcho(){
return testSmackletType;
}

}


This is the ActionScript Code, modified just a little bit:


//Import needed classes
import mx.remoting.Service;
import mx.services.Log;
import mx.rpc.RelayResponder;
import mx.rpc.FaultEvent;
import mx.rpc.ResultEvent;
import mx.remoting.PendingCall;
import mx.remoting.debug.NetDebug
NetDebug.initialize()

// initialize the Logger
var myLogger : Log = new Log (Log.DEBUG, "logger1" );

// override the default log handler
myLogger.onLog = function (message:String):Void {
trace ("myLogger-->>>" + message );
}

//Create the service
myService = new Service ("http://localhost/helloworld/gateway", myLogger, "com.flashdb.services.HelloWorld", null, null);

//on Data handler
function onEchoData (msg:ResultEvent){
Object.registerClass("com.flashdb.domain.SmackletType", SmackletType);
var temp:SmackletType = SmackletType(msg.result);
trace(temp);
}

//on Error handler
function onEchoFault (rs : ResultEvent){
mx.remoting.debug.NetDebug.trace ({level : "None", message : "There was a problem: " + fault.fault.faultstring });
}

//Buttons callbacks
callButton.onPress = function () {
//create the remoting call
var pc : PendingCall = myService.makeEcho();
pc.responder = new RelayResponder(this._parent, "onEchoData", "onEchoFault" );
}

//just clear the textfield
clearButton.onPress = function (){
show.text = "";
}

stop();


In the onEchoData, you can see that I am trying to create a new SmackletType object in Flash and then map my Java obejct that is being passed across to it. However, the trace command returns null, but because of the logger, I can see that all of the data is correctly being passed... this is what is in my output window:


myLogger-->>>7/5 9:31:1 [INFO] logger1: Creating Service for com.flashdb.services.HelloWorld
myLogger-->>>7/5 9:31:1 [INFO] logger1: Creating gateway connection for http://localhost/helloworld/gateway
myLogger-->>>7/5 9:31:1 [INFO] logger1: Successfully created Service
myLogger-->>>7/5 9:31:2 [INFO] logger1: Invoking makeEcho on com.flashdb.services.HelloWorld
null
myLogger-->>>7/5 9:31:2 [INFO] logger1: com.flashdb.services.HelloWorld.makeEcho() returned {_smackletLink: "http://www.google.com",
_smackletName: "Slots",
_smackletTeaseLink: "http://www.google.ca",
_smackletThumbnail: "00.gif"}

Now, if I was to trace(msg) or trace(msg.result) I get [object Object] returned. If I trace(msg.result._smackletType) I will get "Slots". This is just so annoying because I can tell how close I am, but I cannot get this to map properly. If anyone could please help me out it would be a great help to me.

Thank you very much,

--D

dewey
July 5th, 2006, 02:21 PM
Oops! Should have said that I figured it out...

I'm gonna write a tutorial on this stuff... what a bother to get it set up! (Mind you, once it is set up it is an amazing tool!)