Question

I'm having a problem calling an ActionScript method from Java in my native extension. I don't receive any error when building the actually array, populating it with FREObjects and initializing the ActionScript class with the method I want to call. But when I actually call the method, I receive an FREASErrorException.

Java Code:

try {
        FREArray freArray = FREArray.newArray("com.industrycorp.extensions.product.IAPProduct", mProducts.size(), true);

        for (int i = 0; i < mProducts.size(); i++) {
            JavaProduct product = mProducts.get(i);
            FREObject[] freArguments = new FREObject[6];
            freArguments[0] = FREObject.newObject(product.getName());
            freArguments[1] = FREObject.newObject(product.getPrice());
            freArguments[2] = FREObject.newObject(product.getDescription());
            freArguments[3] = FREObject.newObject(product.getType());
            freArguments[4] = FREObject.newObject(product.getIsPurchased());
            freArguments[5] = FREObject.newObject(product.getInventory());

            FREObject freObject = FREObject.newObject("com.industrycorp.extensions.product.IAPProduct", freArguments);
            freArray.setObjectAt(i, freObject);

        }

        FREObject resultHandler = FREObject.newObject("com.industrycorp.extensions.result.ResultHandler", null);
        FREObject[] args = new FREObject[1];
        args[0] = freArray;


        resultHandler.callMethod("receivedProducts", args);



    } catch (Exception e) {
        mContext.dispatchStatusEventAsync("message", "Error converting Java Products to IAP Products: "+e.toString());
    }

The culprit line is: resultHandler.callMethod("receivedProducts", args). I'm not sure what the problem is. The namespaces are correct and the method name is fine.

ActionScript Class:

package com.industrycorp.extensions.result {
import com.industrycorp.extensions.IAPController;

public class ResultHandler {

    public function ResultHandler() {
    }

    public function receivedProducts(products:Array):void {

        IAPController.getInstance().setProducts(products);

    }
}
}

UPDATE

Just for those that are curious. This object works fine.

ActionScript Code

    // Test FREObject Creation
    FREObject testObject = FREObject.newObject("com.industrycorp.extensions.TestObject", null);
    FREObject message = FREObject.newObject("FREObject wOrks");
    FREObject[] testArgs = new FREObject[1];
    testArgs[0] = message;
    testObject.callMethod("PrintTest", testArgs);

Java Code

package com.industrycorp.extensions {
public class TestObject {

    public function TestObject() {
    }

    public function PrintTest(message:String):void {
        trace(message);
    }

}
}

So it's definitely the FREArray/Array as a argument thats the problem.

Was it helpful?

Solution 2

So although the object name is FREArray, the way you initialize it in Java determines if Actionscript expects a Vector or an Array.

Initialization of Array:

FREArray.newArray(int numElements);

Initialization of Vector:

FREArray.newArray(String classname, int numElements, boolean fixed);

It says very clearly in the documentation:

http://help.adobe.com/en_US/air/extensions/WS982b6f491d178e6d6565d9b1132a79a012f-7ff8.html

I just overlooked the fact that the constructor doesn't create either, its specific. Thank you for your help in this issue. I have upvoted your comments.

OTHER TIPS

I don't know if this will work or not since i have no way of testing but it is to big for comments so don't mark down if it is wrong.

Looking over the docs it looks like you are creating a vector array and i think you are getting the error because the parameter in action script is set as an array.

try something like this

    public function receivedProducts(products:Vector.<IAPProduct>):void {
        IAPController.getInstance().setProducts(products);
    }

[EDIT]

Another issue i think i see is this line of code

FREObject[] freArguments = new FREObject[6];

The docs say

public static FREObject newObject( int value )
Creates an FREObject containing a 32-bit signed integer value.

Shouldn't it be

FREArray [] freArguments = new FREArray [6];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top