I have a webview in my app, however sometimes due to connectivity the webview fails to load and I get the default webpage unavailable page. I want to show an alertdialog if the webview failed to load. Is there anyway I can check (maybe in the shouldOverridePageLoad function) that a webview loaded successfully? Thanks again
Use a WebClient on your web view as follow :
webView.setWebViewClient(new WebViewClient(){
#Override public void onReceivedError(WebView view, WebResourceRequest request,
WebResourceError error) {
super.onReceivedError(view, request, error);
// Do something
}
});
Extending on Damien's answer on using WebViewClient, there are four listeners available on WebViewClient to check for the success and failure of loading web pages.
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
}
#Override
public void onReceivedHttpError(
WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler,
SslError error) {
}
});
There is also:
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
}
});
which is deprecated in favor of its overload mentioned in the above code.
Related
I'm trying to load a URL to a webView, but Some time I'm Receiving net::ERR_CONTENT_LENGTH_MISMATCH error in onReceivedError method and the page stopped loading.
The URL is one time generated by the backend and has a lot of redirection.
my webViewClinet :
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
ToastUtil.showToast(error, getContext());
}
Can anyone help me?
I am working on a solution where I am loading a url. I need to get Http Error so that I can reload the url to create a new session.
#Override
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
super.onReceivedHttpError(view, request, errorResponse);
Toast.makeText(getActivity(), "Unexpected error occurred.Reload page again.", Toast.LENGTH_SHORT).show();
if(errorResponse.getStatusCode() == 401){
customWebView.loadUrl(url);
}
}
But the problem with above code is that it work from Marshmallow. Can any one suggest me a solution where I can get error below android version < 23?
onReceivedHttpError
void onReceivedHttpError (WebView view,
WebResourceRequest request,
WebResourceResponse errorResponse)
Notify the host application that an HTTP error has been received from the server while loading a resource.
Can any one suggest me a solution where I can get error below android
version < 23
onReceivedHttpError Added in API level 23. So it will not work below 23.
Try with
.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
/*if(Build.VERSION.SDK_INT<23)
{
// You work
}*/
}
#Override
public void onReceivedHttpError(
WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
/*if(Build.VERSION.SDK_INT >= 23)
{
// Your Work
}*/
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler,
SslError error) {
}
});
FYI
onReceivedError
This method was deprecated in API level 23. Use
onReceivedError(WebView, WebResourceRequest, WebResourceError)
instead.
I got error code if any error in webpage by onReceivedHttpError method of setWebViewClient but when there is no error any page load success or The server callback me code such as 403 then how can i check if status code 200 or 403.
You could use in web view
webView.setWebViewClient(new WebViewClient() {
#SuppressWarnings("deprecation")
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
// Handle the error
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
#Override
public void onReceivedSslError(final WebView view, final SslErrorHandler handler, final SslError error) {
// TODO Auto-generated method stub
super.onReceivedSslError(view, handler, error);
// handle your SSL related error here
// handler.proceed();
}
});
I have some web page which open in WebView.
<body onload="window.location.href='htcmd:loaded';">
After load we open back url "htcmd:loaded" and intercept in code.
Like this:
getWebView().getSettings().setJavaScriptEnabled(true);
getWebView().setWebViewClient(new WebViewClient() {
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if ("htcmd:loaded".equals(url)) {
Toast.makeText(getActivity(), "htcmd:loaded", Toast.LENGTH_SHORT).show();
}
return true;
}
});
getWebView().loadUrl("https://some.url");
On android 4.4.2 in first start all is well. But if I kill app and open after first run, web page not render. But if I tap on screen or change orientation web page appears. Where is problem?
SOLUTION: I have two hacks)))
First: add a java script to web page:
<body onload="setTimeout(function(){window.location.href='htcmd:loaded';},3000);">
Second: add code to web client:
#Override
public void onPageFinished(WebView view, String url) {
if (android.os.Build.VERSION.SDK_INT >= 19) {
view.requestFocus();
}
}
try this
w.getSettings().setLoadWithOverviewMode(true);
w.getSettings().setUseWideViewPort(true);
getWebView().getSettings().setJavaScriptEnabled(true);
getWebView().setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return (false);
});
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
getWebView().loadUrl("https://www.google.com");
I want to trigger a function after webview's finished loading a webpage and received no error. According to my experiment, the onPageFinished() will be triggered several times and in most of the cases, it will be executed before onReceivedError().
So how can I know that the webpage is loaded and no error is received?
You can set the WebViewClient in the WebView and Override its Callback methods which will let you know when the webpage has finished loading the page. For example
webView.setWebViewClient(new MyWebViewClient());
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
super.onPageStarted(view, url, favicon);
if(progess!=null){
progess.dismiss();
}
progess = ProgressDialog.show(LoadUrl.this, "", "Loading please wait...");
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
super.shouldOverrideUrlLoading(view, url);
if (Uri.parse(url).getHost().equals("callback")) {
// This is my web site, so do not override; let my WebView load the page
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url){
super.onPageFinished(view, url);
// Add YOUR CODE HERE
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
Toast.makeText(LoadUrl.this, "Sorry! " + description, Toast.LENGTH_SHORT).show();
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
super.onReceivedSslError(view, handler, error);
handler.proceed();
}
}
Here onPageFinished(WebView view, String url) will be called when webView has finished loading the current Page.
Hope it helps :)