In an Android web view, I'm trying to get the URL of webpages visited by the user. I'm using:
public void onPageStarted(WebView view, String url, Bitmap favicon)
public boolean shouldOverrideUrlLoading(WebView view, String url)
public void onPageFinished(WebView view, String url)
Unfortunately, for certain cases all of the above return me the wrong URL. For example when I'm on http://www.yahoo.com I get the correct URL, but once I click on any news article, the URL returned to me is still http://www.yahoo.com.
Just to confirm, I opened http://www.yahoo.com on Chrome, and then tapped the same articles to check whether Chrome gets the correct URLs. It does, which means it's possible and I'm probably missing something fundamental.
How can I get that URL in my own webview? Note that I have tried the JavaScript interface as well, but it's been of no use.
Related
While loading a URL in webView there are multiple of HTTP request made while loading a single page. Is there a way to track those requests? The closest I could get is to use
public boolean shouldOverrideUrlLoading(WebView view, String url)
method to view the URL requests made but could not capture the packets each page make.
I know this Question has already been asked years ago but couldn't find a solid answer that I can depend. Any help would be helpful
You can override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request)
method of WebViewClient. This will be called everytime javascript code requests any resource inside Webview.
I have a page that displays a news story, the page's content is loaded from a json response which is cached, the problem occurs when the uses is not connected to the internet and tries to access the story, the story loads correctly but the iFrame shows an ER_INTERNET_DISCONNECTED error.
We want to still be able to use the offline feature but if the iFrame has any error remove it from the page, is there anyway to do this ?
I've uploaded a screenshot if that helps
enter image description here
webView.setWebViewClient(new WebViewClient() {
#Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
// either load a new url, or show a popup to the user
webView.loadUrl("file:///android_asset/custom_error.html");
} });
You can decide for yourself what to do in the onReceivedError
I have an Activity in my app which has a Webview in it which opens a URL provided to it and is then redirected to the landing page of the website. The issue I'm facing is once the user is on landing page the redirection is same page redirection i.e. if the landing page is
www.example.com/landing
The redirection is
www.example.com/landing/#other_page
Which does not call the
onPageStarted(WebView view, String url, Bitmap favicon)
of the webview, but
onPageFinished(WebView view, String url)
is called.
Is there a way to track when the redirect is starting to load since I need to log the time required to load a page even if the page is same page redirection.
Thanks in advance!
Edit: According to the documentation:
onPageStarted will not be called when the contents of an embedded frame changes, i.e. clicking a link whose target is an iframe, it will also not be called for fragment navigations (navigations to #fragment_id).
Which is exactly my case. Is there any other way to track the start and end of loading of fragment navigation?
I think you are looking for that method
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("Webview", url);
return false;
}
});
In my mobile site I have links to other websites that also have apps. when my users click on the links they get options in Android (I don't know about iPhone) to go to the app or continue with the browser. How do I prevent the user from going to the apps and just cntinue with the browser after clicking my links without seeing this menu (browser/app)?
What you're asking is how to prevent the expected behavior of a webpage you load in your app. That's generally not good UI/UX design. But, to do so you would need to set a WebViewClient on your WebView and evaluate the url that is being loaded to see if it is for the download of an app:
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
if (url.contains("play.google.com")) {
//handle alternate action here
}
}
#Override
public void onPageFinished(WebView view, String url){
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
}
});
That's something that's been set up on the 3rd party site, not something that's being done by the OS. There's no way to control the behavior of the browser after the user has left your web site.
For example LinkedIn will do this but Last Pass does not, even though they also have an app. This is something LinkedIn has chosen to do with their site; you can't do anything about that from your web site.
When is shouldOverrideUrlLoading method called?
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
}
});
Is it called during initial loading of url? e.g. webView.loadUrl( "file:///android_asset/html/index.html");
Is it called everytime URL of webview changes?
Any reference? I didn't find one. Thanks
It does however, get called when the WebView to load a different URL from the one the user had requested.
Calling loadUrl() will also trigger the shouldOverrideUrlLoading() method. (Only when a new url is about to be loaded.)
Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView. If
WebViewClient is not provided, by default WebView will ask Activity
Manager to choose the proper handler for the url. If WebViewClient is
provided, return true means the host application handles the url,
while return false means the current WebView handles the url.
Ref : public boolean shouldOverrideUrlLoading (WebView view, String url)
Below is the answer for your both the questions:
As per the document, it will manage every time new URL is about to load in current WebView.