简体   繁体   中英

How to get Url of the WebView using WebResourceRequest in Java?

I have these codes:

   public class MyAppWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            //want url of the webview in here
        }

I just want to know, how can I obtain the Url of the webView to override the Url loading. The shouldOverrideUrlLoading(WebView view, String url) method is depreceated in API21.

Thanks in advance!

in \\Android\\sdk\\sources\\android-25\\android\\webkit\\WebViewClient.java , you can see this code.

So short answer to your question is: request.getUrl().toString()

Check sources on GrepCode .

boolean shouldOverrideUrlLoading (WebView view, String url) deprecated in API 24, boolean shouldOverrideUrlLoading (WebView view, WebResourceRequest request) added in API level 24

        @RequiresApi(api = Build.VERSION_CODES.N)
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            if(Uri.parse(request.getUrl().toString()).getHost().endsWith("google.com")) {
                //The host application wants to leave the current WebView and handle the url itself. 
                return true;
            }

            if(Uri.parse(request.getUrl().toString()).getHost().endsWith("yandex.com")) {                    
                //The current WebView handles the url. 
                return false;
            }               

            if(Uri.parse(request.getUrl().toString()).getScheme().equals("file")) {
                //The current WebView handles the url. 
                return false;
            }

            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(request.getUrl().toString()));
            view.getContext().startActivity(intent);
            return true;
        }
private String cUrl = "";

cUrl = webView.getUrl();

Then you can use it wherever you want.

please see my answer may it will help you...!!!!

WebView webview = new WebView(context);
webview.setWebViewClient(new WebViewClient()
        {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);

                System.out.println("your current url when webpage loading.." + url);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                System.out.println("your current url when webpage loading.. finish" + url);
                super.onPageFinished(view, url);
            }

            @Override
            public void onLoadResource(WebView view, String url) {
                // TODO Auto-generated method stub
                super.onLoadResource(view, url);
            }
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                System.out.println("when you click on any interlink on webview that time you got url :-" + url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });

how to get the current page url from the web view in android

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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