Android WebView not loading URL

前端 未结 10 940
鱼传尺愫
鱼传尺愫 2020-12-05 02:15

I want to load the URL in WebView

I have used the following Code:

webView = (WebView) findViewById(R.id.webview1);
webView.setWebViewCli         


        
10条回答
  •  一个人的身影
    2020-12-05 02:40

    Use this it should help.

     var currentUrl = "google.com" 
     var partOfUrl = currentUrl.substring(0, currentUrl.length-2)
    
     webView.setWebViewClient(object: WebViewClient() {
    
     override fun onLoadResource(WebView view, String url) {
     //call loadUrl() method  here 
     // also check if url contains partOfUrl, if not load it differently.
     if(url.contains(partOfUrl, true)) {
         //it should work if you reach inside this if scope.
     } else if(!(currentUrl.startWith("w", true))) {
         webView.loadurl("www.$currentUrl")
    
     } else if(!(currentUrl.startWith("h", true))) {
         webView.loadurl("https://$currentUrl")
    
     } else { 
       //...
     }
     }
    
     override fun onReceivedSslError(view: WebView?, handler: SslErrorHandler?, error: SslError?) {
      // you can call again loadUrl from here too if there is any error.
    }
     // You should also override other override method for error such as
     // onReceiveError to see how all these methods are called one after another and how
    // they behave while debugging with break point. 
    } 
    

提交回复
热议问题