0
  • Solution must work on API >20
  • This solution does not work. I assume it works only on lower versions of the Android API. I'm testing on Android 8.0.
  • I've tried using Retrofit as soon as the page is loaded, instead of getting the html via WebViewClient. But the user is not logged in in the Retrofit request. And I'm doing the same request 2 times then.

    @SuppressLint("JavascriptInterface")
    private fun initializeWebView(url : String?) {
        binding.webView.loadUrl(url)
        binding.webView.settings.javaScriptEnabled = true
        binding.webView.settings.useWideViewPort = true
        binding.webView.requestFocus(View.FOCUS_DOWN)
        binding.webView.addJavascriptInterface(MyJavaScriptInterface(context!!), "HtmlViewer");
    
        binding.webView.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
                return false
            }
    
            override fun onPageFinished(view: WebView?, url: String?) {
    //              super.onPageFinished(view, url)
    
            //tried adding a sleep, but doesn't work:
            Thread.sleep(6000)
    
                   //showHTML method is not being called:                
     binding.webView.loadUrl("javascript:window.HtmlViewer.showHTML" +
                        "('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');")
    
     // Prints out: html: null
                binding.webView.evaluateJavascript(
                        "javascript:window.HtmlViewer.showHTML" +
                                "('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');"
                ) { html ->
                    Log.d("HTML", html)
                    // code here
                }
            }
        }
    }
    
    
    class MyJavaScriptInterface(val context: Context) {
    
      fun showHTML(html: String) {
        Log.d("",""+html)
    
        AlertDialog.Builder(context).setTitle("HTML").setMessage(html)
          .setPositiveButton(android.R.string.ok, null).setCancelable(false).create().show()  }
    }
    
Jim Clermonts
  • 1,694
  • 8
  • 39
  • 94
  • What exactly does "does not work" mean? What are your precise symptoms? Note that if the Web page is doing asynchronous work (e.g., AJAX-style requests), `onPageFinished()` will be called before that work completes. – CommonsWare Oct 28 '18 at 21:46
  • showHTML method is not being called. evaluateJavascript prints out "null". Webpage is very modern so I assume it does async work, what approach should I use? – Jim Clermonts Oct 28 '18 at 22:38
  • You would need to come up with some other trigger, one that can determine whether the Web page is completely loaded. `WebView` is designed for a user to use, so the typical trigger is the user tapping some button to say "do something with the HTML now". – CommonsWare Oct 28 '18 at 22:59
  • I don't think that's right: I've added a Thread.sleep(6000), and put a breakpoint there. I'm sure the page is fully loaded and still the html contents are null. – Jim Clermonts Oct 29 '18 at 07:21

0 Answers0