如何从android的web视图中获取当前页面的url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23827460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:30:13  来源:igfitidea点击:

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

androidurlwebview

提问by Nikhila Benjaram

Using webview.loadUrl(url)method I open an url. If I click any Buttonon the view it directs to another page. Now I want to get the url of the directed page. how can I get it?

使用webview.loadUrl(url)方法我打开一个网址。如果我单击Button视图上的任何内容,它会指向另一个页面。现在我想获取定向页面的 url。我怎么才能得到它?

I also want the content that is displayed on the webview. How to get the content of the WebView?

我还想要显示在 webview 上的内容。如何获取 的内容WebView

回答by V.P.

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);

                Log.d("WebView", "your current url when webpage loading.." + url);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                Log.d("WebView", "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);
            }
        });

回答by Vinayak Bevinakatti

String webUrl = webView.getUrl();

回答by Spring Breaker

I am assuming that you have set your WebViewClient.If not then you can do like below,

我假设你已经设置了你的WebViewClient。如果没有,那么你可以像下面那样做,

webView.setWebViewClient(new MyWebViewClient());

String currentUrl;

private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {         
            currentUrl=url;
            return true;
        }
    }

Here when you click on any link on the WebViewthen it will call shouldOverrideUrlLoading()and you can get the current url there in currentUrl.

在这里,当您单击 上的任何链接时WebView,它将调用shouldOverrideUrlLoading(),您可以在currentUrl.

回答by akemko

this is the most simple way of handling this for API > 20: There are obviously other methods with parsers like HTMLCleaner to have more control. However, this is good and simple for getting URL. Each time URL changes in Webview, URL in the request object changes as well.

这是处理 API > 20 的最简单方法:显然还有其他方法可以使用解析器(如 HTMLCleaner)来获得更多控制。但是,这对于获取 URL 来说既好又简单。每次 Webview 中的 URL 更改时,请求对象中的 URL 也会更改。

@Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            String temp  = request.getUrl().toString();
            if(temp.isEmpty()){
                Log.i(ActivityName, "No url returned!");
            }else{
                Log.i(ActivityName, temp);
            }
            return  false;
        }

you can also make your call by overriding onpagefinished method of webviewclient. I wanted to note this, since this is called when page finishes loading.

您还可以通过覆盖 webviewclient 的 onpagefinished 方法来拨打电话。我想注意这一点,因为这是在页面加载完成时调用的。