What are valid return values for a Javascript Interface on an Android WebView?

browep picture browep · Feb 2, 2012 · Viewed 17.8k times · Source

I have an Android WebView that has JavaScript that is calling Android methods through the addJavascriptInterface method:

myWebview.addJavascriptInterface(new JavascriptBridge(), "Android");

public class JavascriptBridge {

    public String getAString() {          
        return "my_str";
    }
}

This works fine. I want to return a list of ints to the WebView. Tried this:

public class JavascriptBridge {

    public int[] getMyInts() {          
        return new int[]{1,2,3};
    }
}

but calling this function in JS returns undefined:

var myInts = Android.getMyInts();

Is there a list of valid return types for an Android Javascript Interface? Is it only primitives?

Answer

slay picture slay · Mar 1, 2012

I have not seen a list of valid types (for passing values to Java functions and to return), but only primitives and string seem to work.

You can use JSON (e.g. stringify and parse in Javascript, check various Java options at json.org