如何在 Android WebView 页面加载完成后获取并使用当前 URL。

huangapple go评论75阅读模式
英文:

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

问题

我试图在mypage加载完成后使用返回的URL,并将其与另一个相同的URL进行比较。

以下是我的代码:

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

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

似乎当两个链接不相同时,只有doFunction2()会被调用。

(页面重定向到成功页面,我只想检查页面是否真的已重定向)

英文:

im trying to use the returned url after mypage finishes loading and comparing it with another url which is the same.

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.

(The page redirects to the success page and I just wanna check if the page really redirected or not)

答案1

得分: 0

我假设您已经设置了您的 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;
    }
}

在这里,currentUrlLink 是一个字符串,用于获取 webView 的当前 URL。使用 .equals() 您可以将 success 字符串与当前 URL 进行匹配。

英文:

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

   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. and using .equals() you can match the success string with the current URL.

huangapple
  • 本文由 发表于 2020年10月16日 22:02:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64390730.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定