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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
Never seen any such documentation. Basic data types is what I've seen work - numbers and strings.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.