简体   繁体   English

Webview ShouldOverrideUrlLoading未调用

[英]Webview ShouldOverrideUrlLoading not called

I have a webview like the below given code, i am trying to catch the redirect url on login inside the webview, so i have used ShouldOverrideUrlLoading method but the control never comes inside this method. 我有一个像下面给定代码的webview,我试图在webview内登录时捕获重定向URL,所以我使用了ShouldOverrideUrlLoading方法,但是控件从未出现在此方法内。

I have read other answers related to this and tried few things like using webView.loadDataWithBaseURL the webview went blank and trying to use chromeclient and catching the url but this works only on first load not on redirecting. 我已经阅读了与此相关的其他答案,并尝试了一些操作,例如使用webView.loadDataWithBaseURL,webview变为空白,并尝试使用chromeclient并捕获url,但这仅在首次加载时有效,而在重定向时不起作用。

what might be the issue? 可能是什么问题?

    WebView webView = (WebView) findViewById(R.id.loginwebview);
    webView.getSettings().setLoadsImagesAutomatically(true);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setAllowContentAccess(true);

    webView.loadUrl("https://www.xfinityprepaid.net/CustomerActivation/CustomerActivation");

    webView.getSettings().setUseWideViewPort(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setDomStorageEnabled(true);
    webView.clearView();
    webView.setHorizontalScrollBarEnabled(false);
    webView.getSettings().setAppCacheEnabled(true);
    webView.getSettings().setDatabaseEnabled(true);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        webView.getSettings().setDatabasePath("/data/data/" + this.getPackageName() + "/databases/");
    }
    webView.setVerticalScrollBarEnabled(false);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setDisplayZoomControls(false);
    webView.getSettings().setAllowFileAccess(true);
    webView.getSettings().setPluginState(WebSettings.PluginState.OFF);
    webView.setScrollbarFadingEnabled(false);
    webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
    webView.setWebViewClient(new WebViewClient());
    webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
    webView.setInitialScale(1);

    webView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 
             {
                Log.d("url",url);
                if (url.contains("account-secured/dashboard")){
                    startActivity(new Intent(LoginActivity.this,MainActivity.class));
                    finish();
               }else {
                    view.loadUrl(url);
                }
            }


            return false;
        }

    });

If your links are local try adding an app:// prefix or equivalent. 如果您的链接是本地链接,请尝试添加app://前缀或等效名称。

From Migrating to WebView in Android 4.4 Android 4.4中的迁移到WebView

For example, the new WebView may not call your shouldOverrideUrlLoading() method for links like this: 例如,对于这样的链接,新的WebView可能不会调用您的shouldOverrideUrlLoading()方法:

 <a href="showProfile">Show Profile</a> 

Instead of using a simple string in a link as shown above, you can use a custom scheme such as the following: 您可以使用如下自定义方案来代替在链接中使用简单的字符串:

 <a href="example-app:showProfile">Show Profile</a> 

This worked for me when I changed the link from <a href="#4"> to <a href="app://4"> . 当我将链接从<a href="#4">更改为<a href="#4">时,这对我<a href="app://4">

class MyWebViewClient(val activity: AppCompatActivity) : WebViewClient() {
    override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
        activity.findViewById<RadioButton>(AMAinActivity.primaryButtons[3]).performClick()
        return true
    }
}

Note that the method works even though it is deprecated. 请注意,即使该方法已弃用,该方法也能正常工作。

To intercept your link click specify the link with its full path ie <a href="https://www.xfinityprepaid.net/CustomerActivation/CustomerActivation" >:- 要拦截您的链接,请指定具有完整路径的链接,即<a href="https://www.xfinityprepaid.net/CustomerActivation/CustomerActivation" :-

尝试覆盖onReceivedSslError。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM