2

I'm trying to access my Java method helloWorld(); using JavaScriptInterface in Android, from the page I'm viewing using WebView

I'm a little new to this, and I don't know why I'm not getting the value to my html page

This is the Android code I'm using:

WebView web;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    web = (WebView) findViewById(R.id.webview01);
    JavaScriptInterface jsInterface = new JavaScriptInterface(this);
    web.getSettings().setJavaScriptEnabled(true);
    web.addJavascriptInterface(jsInterface, "JSInterface");
    web.getSettings().setUseWideViewPort(true);
    web.getSettings().setLoadWithOverviewMode(true);
    web.setWebViewClient(new myWebClient());
    web.getSettings().setJavaScriptEnabled(true);
    web.loadUrl("http://exampleweb:10110/exWeb/deposits.jsp");
}

public class myWebClient extends WebViewClient
{
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub

        view.loadUrl(url);
        return true;

    }
}
public class JavaScriptInterface {
    private Activity activity;

    public JavaScriptInterface(Activity activity) {
        this.activity = activity;
    }
    @JavascriptInterface
    public String helloWorld(){
        return("hello world");
    }
}

}

This is the JavaScript code I've been using (on the page I'm viewing with WebView)

HTML button

<td> <button type="button" class="btn btn-xs btn-primary connectToAndroid">Test android</button></td>

JavaScript

$(".connectToAndroid").on('click', function () {
var val = window.JSInterface.helloWorld();
alert(val);
});

Does anyone know what I'm doing wrong here? I just want to alert an "Hello World" from android in my HTML page.

Any pointers are greatly appreciated!

7
  • 1
    Try putting a log in helloWorld and see if its being called. Commented Jan 29, 2016 at 10:51
  • 1
    why do you call it by WINDOW.JSinterface and not directly by JSInterface ? Commented Jan 29, 2016 at 10:58
  • @r4phG I don't know why actually, I've been trying to follow some guides on the internet. I'll try without "window". Commented Jan 29, 2016 at 11:06
  • 1
    @Coderap Have You tried ? :D Commented Jan 29, 2016 at 13:17
  • 1
    did you load jquery before using it? try to use javascript instead of jquery and try again. Commented Feb 1, 2016 at 8:31

2 Answers 2

1

Try by adding this line of code before loading the url and make sure you reference your jQuery library in the html.

web.setWebChromeClient(new WebChromeClient(this));
Sign up to request clarification or add additional context in comments.

2 Comments

I will give you an upvote, not because you solved the answer, but this did help me with displaying the " alert ". When I managed to pass the data, the alert didn't work yet, but by adding this line of code, I could alert the data aswell. Thank you :)
@Coderap Glad that I helped :)
1
+200

This is very weird, I tried using this a couple of hours ago:

$(".connectToAndroid").on('click', function () {
  var val = Android.helloWorld();
  alert(val);
 });

Then I changed it, it didn't say Android anymore,

yet I kept getting an error saying:

Uncaught ReferenceError: Android is not defined

And after a while I figured, Android.helloWorld(); must be cached somehow in my webview app or something.

So, I changed back to this:

$(".connectToAndroid").on('click', function () {
  var val = Android.helloWorld();
  alert(val);
 });

And added this:

web.addJavascriptInterface(jsInterface, "Android");

And now it works like a charm, as it should have from the beginning if it wasn't for some cache somewhere,

Can anyone explain why this happens? Does android webview actually cache javascript code? And can I prevent this behaviour in the future somehow? Cache bothers a lot when coding/testing new stuff.

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.