onReceivedHttpError : support for android version < 23 - android

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.

Related

Receiving net::ERR_CONTENT_LENGTH_MISMATCH error in android webView

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?

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

android webview timeout error callback

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

Android WebView not render of web page

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");

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