WebView.getUrl() returns null because page not done loading

痞子三分冷 提交于 2019-12-11 03:42:39

问题


I want to get the url of the webview. However, the method calls before the page is done loading so it always returns with null. Any way around this? Thanks.

WebView webView = new WebView(this);
setContentView(webView);

webView.loadUrl(myURL);

//page is not done loading yet
String url = webView.getUrl(); //returns null

回答1:


Try adding a WebViewClient and overriding the onPageFinished(...) method. I've never done it but something like this might work...

String theUrl;
WebView webView = new WebView(this);
setContentView(webView);

webview.setWebViewClient(new WebViewClient() {

    public void onPageFinished(WebView view, String url) {
        theUrl = url;
    }

});

webView.loadUrl(myURL);



回答2:


Create a subclass of WebViewClient which overrides onPageStarted(webView, url, favicon) and set it to your WebView (using setWebViewClient()).
You'll have the url of the page which is loading or displayed.



来源:https://stackoverflow.com/questions/13773037/webview-geturl-returns-null-because-page-not-done-loading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!