Calling Javascript method from within webview in Android

Viewed 16945

I am trying to call a javascript method defined in a html page from within webview. Function is not being called, and I don't see any errors in the log.

This is html file.

    </head>
    <body>
    <script type="text/javascript">
        function callJS(){
            $.ajax({url:"http://10.0.2.2:5010"});
        }
    </script>
    </body>
    </html>

And this is the Java code in a Activity in android

    WebView webView = new WebView(this);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("file:///android_asset/temp.html");
    webView.loadUrl("javascript:callJS()");

Not sure how to debug this. When I add a onload=callJS() in body tag in html, I see the remote call being made. So, it looks like my HTML is fine,and it is being loaded into webview. However, webview is not able to call javascript method directly.

3 Answers
Related