英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论