最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Passing Data From Javascript To Android WebView - Stack Overflow

programmeradmin1浏览0评论

I have written a piece of Java code which is intended as to call Java code from Javascript by a android WebView gateway.

webView = (WebView) findViewById(R.id.captchaView);
WebSettings ws = webView.getSettings();
ws.setJavaScriptEnabled(true);
webView.loadUrl("/");

webView.setWebViewClient(BrowserHandler);
webView.addJavascriptInterface(new IJavascriptHandler(), "cpjs");

WebViewClient BrowserHandler = new WebViewClient() {
   @Override
   public void onPageFinished(WebView view, String url) {
      view.loadUrl("javascript:window.cpjs.onPageLoaded(document.body.innerHTML);void(0);");
   }
};

final class IJavascriptHandler {
   IJavascriptHandler() {
   }

   public void onPageLoaded(String html) {
      // this is called from JS
      Toast t = Toast.makeText(getApplicationContext(), "Yes", 2000);
      t.show();
   }
}

The above code checks for web page load and once the page is finished loading, it will call javascript function which is exposed from Java so it could carry what I need from the page.

This is not working as expected what it does is simply refresh the page every few seconds. I am not sure what is wrong here?

I have written a piece of Java code which is intended as to call Java code from Javascript by a android WebView gateway.

webView = (WebView) findViewById(R.id.captchaView);
WebSettings ws = webView.getSettings();
ws.setJavaScriptEnabled(true);
webView.loadUrl("https://google./");

webView.setWebViewClient(BrowserHandler);
webView.addJavascriptInterface(new IJavascriptHandler(), "cpjs");

WebViewClient BrowserHandler = new WebViewClient() {
   @Override
   public void onPageFinished(WebView view, String url) {
      view.loadUrl("javascript:window.cpjs.onPageLoaded(document.body.innerHTML);void(0);");
   }
};

final class IJavascriptHandler {
   IJavascriptHandler() {
   }

   public void onPageLoaded(String html) {
      // this is called from JS
      Toast t = Toast.makeText(getApplicationContext(), "Yes", 2000);
      t.show();
   }
}

The above code checks for web page load and once the page is finished loading, it will call javascript function which is exposed from Java so it could carry what I need from the page.

This is not working as expected what it does is simply refresh the page every few seconds. I am not sure what is wrong here?

Share Improve this question asked Feb 5, 2011 at 9:46 Umair A.Umair A. 6,87320 gold badges85 silver badges134 bronze badges 1
  • will you please send me the updated code. thanks in advance – Harshil Kulkarni Commented Feb 22, 2017 at 4:14
Add a ment  | 

1 Answer 1

Reset to default 2

Well, there are lots of things that are rather strange in what you have done.

  • From Java, you are triggering Javascript...to call back into Java. You are assuming this is safe.

  • You are loading the Google home page, which will perform a redirect when it encounters an Android browser, the last time I checked.

  • You are attempting to display a Toast, yet you have no idea if that code will run on the main application thread or not.

发布评论

评论列表(0)

  1. 暂无评论