android webview timeout error callback - android

These are the callback I have for the WebView, but none of them returns me a TimeOut Error, is there any other I can add in order to get them?
webView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
webViewError = true;
errorMessageView.setText(getString(R.string.unknown_error) + " " + error.toString());
}
#Override
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
super.onReceivedHttpError(view, request, errorResponse);
webViewError = true;
errorMessageView.setText(getString(R.string.unknown_error) + " " + errorResponse.toString());
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
super.onReceivedSslError(view, handler, error);
webViewError = true;
errorMessageView.setText(getString(R.string.ssl_error));
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
setUpView();
}
});

The onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) you have used will get called only if the device has Android M.
To support other versions, you have to use the deprecated onReceivedError(WebView view, int errorCode, String description, String failingUrl).
Like this:
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
//Handle the error
super.onReceivedError(view, errorCode, description, failingUrl);
}
#TargetApi(Build.VERSION_CODES.M)
#Override
public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr){
onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());
}

Related

WebView: How to load .php file in the 8.0 WebView

i am trying load a .php file in WebView. it was working properly before API level 28 after i updated to API level 28 its not working its showing white screen. nothing is showing i tried all the options.
Here is the code
I am added the safe browsing false in the manifest file also.
I tried searching in the google nothing is helped me if any one done please help in this case to resolve.
String url="https://xxx/xxx/abc.php";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handleSSLHandshake();
WebView mWebView = (WebView) findViewById(R.id.webView);
// PackageInfo webViewPackageInfo = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// webViewPackageInfo = WebView.getCurrentWebViewPackage();
mWebView.getSettings().setSafeBrowsingEnabled(false);
// Log.d("MY_APP_TAG", "WebView version: " + webViewPackageInfo.versionName);
}
WebViewClientImpl webViewClient = new WebViewClientImpl(this);
mWebView.setWebViewClient(webViewClient);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.getSettings().setBuiltInZoomControls(false);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(url);
if (18 < Build.VERSION.SDK_INT ){
//18 = JellyBean MR2, KITKAT=19
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
}
}
public class WebViewClientImpl extends WebViewClient {
private Activity activity = null;
public WebViewClientImpl(Activity activity) {
this.activity = activity;
}
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
webView.loadUrl(url);
// Log.i(TAG,url);
return true;
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
}
}
I solved my problem. it was the certificates issues, i just added the following code, its working fine now
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}

Why is android webview onPageStarted being called but onPageFinished is not?

webview.setVisibility(View.VISIBLE);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setUseWideViewPort(true);
webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String URL, Bitmap favicon)
super.onPageStarted(view, url, favicon);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description,failingUrl);
}
#Override
public void onPageFinished(WebView webview, String url) {
super.onPageFinished(webview, url);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
});webview.loadUrl(“my_url”);

Android:How to get status code 200 OK in Webview?

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();
}
});

Webview doesn't load page on phone screen but on tablet

I don't understand why if I try to load a page on a WebView on my phone it doesn't load but on tablet it works.
The code of the app:
mWebView.getSettings().setJavaScriptEnabled(false);
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
}
#Override
public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
});
mWebView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
mProgressBar.setVisibility(View.GONE);
} else {
mProgressBar.setVisibility(View.VISIBLE);
mProgressBar.setProgress(newProgress);
}
}
});
A restriction on the website?

How to check if webview failed to load page (android)?

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.

Categories

Resources