5

Is there any documentation regarding the WebView JavaScript Bridge? I am looking for documentation that describes the capabilities and supported data types for methods defined within the "JavascriptInterface".

For example if I define the following:

public class JavaScriptInterface {

    public int incrementNumber(int num) {
       return num + 1;

}

If I call this method from within JavaScript and run it in the emulator, everything seems to work fine. If I run this on my NexusOne, the passed in "num" argument is always "0".

If I change the above to:

 public class JavaScriptInterface {

    public int incrementNumber(String num) {
       // Leaving out try/catch
       int tempNum = newRadius = Integer.parseInt(num);
       return tempNum + 1;

}

... everything seems to work. So I am wondering if JavaScriptInterface method arguments should/can only be of type String?

Relevant resources: http://developer.android.com/reference/android/webkit/WebView.html http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface(java.lang.Object, java.lang.String) http://code.google.com/apis/maps/articles/android_v3.html

tamsler
  • 1,275
  • 1
  • 18
  • 26

2 Answers2

1

You can either require String args on the Java side or ensure that numbers are actual numbers (and not text versions of numbers - see about.com - JavaScript: Strings to Numbers) on the JavaScript side.

typo.pl
  • 8,812
  • 2
  • 28
  • 29
  • Thank you for the information. But is there a document that outlines the supported data types that can be passed in to the JavaScriptInterface methods as well as supported return types. – tamsler Feb 08 '11 at 02:14
  • Never seen any such documentation. Basic data types is what I've seen work - numbers and strings. – typo.pl Feb 08 '11 at 02:46
1

The only relevant official doc is here: http://developer.android.com/guide/webapps/webview.html But no description about the available types

standup75
  • 4,734
  • 6
  • 28
  • 49