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

后端 未结 4 471
-上瘾入骨i
-上瘾入骨i 2020-12-09 08:32

Using webview.loadUrl(url) method I open an url. If I click any Button on the view it directs to another page. Now I want to get the url of the dir

相关标签:
4条回答
  • 2020-12-09 08:53

    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);
                }
            });
    
    0 讨论(0)
  • 2020-12-09 08:53

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

    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 WebView then it will call shouldOverrideUrlLoading() and you can get the current url there in currentUrl.

    0 讨论(0)
  • 2020-12-09 08:56

    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.

    @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.

    0 讨论(0)
  • 2020-12-09 08:59
    String webUrl = webView.getUrl();
    
    0 讨论(0)
提交回复
热议问题