简体   繁体   English

在android WebView中完成页面加载后如何获取和使用当前url

[英]how to get and use the current url after the page is finiahed loading in android WebView

im trying to use the returned url after mypage finishes loading and comparing it with another url which is the same.我试图在 mypage 完成加载后使用返回的 url 并将其与另一个相同的 url 进行比较。

here is my code:这是我的代码:

String success ="https://example.com/success";

public void onPageFinished(WebView webv, String url){
    if(success==url){
        doFunction1();
    }else{
        doFunction2();
    }           
}

it seems like the two Links are not the same then only doFunction2() is called.似乎两个链接不一样,然后只doFunction2()

(The page redirects to the success page and I just wanna check if the page really redirected or not) (页面重定向到成功页面,我只想检查页面是否真的重定向了)

I am assuming that you have set your WebViewClient.If not then you can do like below, You can use this method to achieve what you want我假设你已经设置了你的 WebViewClient。如果没有,那么你可以像下面那样做,你可以使用这个方法来实现你想要的

   webView.setWebViewClient(new MyWebViewClient());

String success ="https://example.com/success";


private class MyWebViewClient extends WebViewClient {
       @Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            String currentUrlLink  = request.getUrl().toString();

           if(success.equals(currentUrlLink)){
                    doFunction1();
        }else{
            doFunction2();
        }     
            return false;
        }
    }

here currentUrlLink is the string where it gets the webView's current URL.这里 currentUrlLink 是它获取 webView 的当前 URL 的字符串。 and using .equals() you can match the success string with the current URL.并使用.equals()您可以将success字符串与当前 URL 匹配。

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

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