user1065490
user1065490

Reputation: 305

Android webview onReceivedError() not working

I'm trying to show an alert box instead of 'Web page not available' error page on my web view but not working even after adding onReceivedError() method as per documentation, please suggest me if I'm missing something, Here is my code...

public class CloudPageHolder extends Activity {
WebView mWebView;
ProgressDialog _dialog ;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mWebView = (WebView) findViewById(R.id.webview);


    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setDomStorageEnabled(true);
    mWebView.loadUrl("file:///android_asset/www/MyPage.html");
    mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
    mWebView.getSettings().setPluginsEnabled(false);
    mWebView.getSettings().setSupportMultipleWindows(false);
    mWebView.getSettings().setSavePassword(false);
    mWebView.getSettings().getAllowFileAccess();
    mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    mWebView.getSettings().setUseWideViewPort(true);
    mWebView.getSettings().setLoadWithOverviewMode(true);

    mWebView.setWebViewClient(new WebViewClient() {  
        public void onReceivedError(WebView view, int errorCode, String description,     String failingUrl) {  
          new AlertDialog.Builder(CloudPageHolder.this)  
                .setMessage("Something went wrong!")  
                .setPositiveButton(android.R.string.ok,  
                        new AlertDialog.OnClickListener()  
                        {  
                           public void onClick(DialogInterface dialog, int which)  
                            { 

                               dialog.dismiss();
                            }  
                        })  
                .setCancelable(false)  
                .create()  
                .show();  
                }  
        });  


    mWebView.setWebChromeClient(new WebChromeClient() {  
        @Override  
        public boolean onJsAlert(WebView view, String url, final String message, final android.webkit.JsResult result)  
        {  
            new AlertDialog.Builder(CloudPageHolder.this)  
                .setMessage(message)  
                .setPositiveButton(android.R.string.ok,  
                        new AlertDialog.OnClickListener()  
                        {  
                           public void onClick(DialogInterface dialog, int which)  
                            { 
                               result.confirm();
                            }  
                        })  
                .setCancelable(false)  
                .create()  
                .show();  

            return true;  
        } 
  });
 }

Thanks in Advance...

Upvotes: 3

Views: 9295

Answers (2)

Squatting Bear
Squatting Bear

Reputation: 1724

I eventually found a way of doing this, but it isn't pretty. You can load the page in javascript via an XMLHttpRequest, which lets you access the status code. This probably messes up the progress notifications and may have other undesirable behaviour too. But it was acceptable in my app, so it may be of some help to others.

public class StatusNotifyingWebView extends WebView {

    public interface OnStatusReceivedListener {
        void onStatusReceived(int statusCode);
    }

    private class JsInterface {
        public void notifyRequestStatus(final int statusCode) {
            getHandler().post(new Runnable() {
                @Override
                public void run() {
                    mOnStatusReceivedListener.onStatusReceived(statusCode);
                }
            });
        }
    }

    private OnStatusReceivedListener mOnStatusReceivedListener;

    public void setOnStatusReceivedListener(final OnStatusReceivedListener listener) {
        mOnStatusReceivedListener = listener;
        addJavascriptInterface(new JsInterface(), "statusNotifyJsInterface");
    }

    public void loadUrlWithStatusEvent(final String url) {
        Assert.assertNotNull(mOnStatusReceivedListener);
        final String script =
                "  var httpRequest = new XMLHttpRequest();\n"
                + "httpRequest.open('GET', '" + url + "', false);\n"
                + "try { httpRequest.send(null); } catch (err) { }\n"
                + "statusNotifyJsInterface.notifyRequestStatus(httpRequest.status);\n"
                + "document.write(httpRequest.responseText);\n"
                + "document.close();\n";

        final String html = "<html><head><script>" + script + "</script></head><body/></html>";
        loadDataWithBaseURL(url, html, "text/html", "UTF-8", null);
    }
}

Upvotes: 1

Romina Liuzzi
Romina Liuzzi

Reputation: 741

I am not sure of what your error is but the documentation of onReceivedError is somewhat missleading. At this point you CANNOT intercept HTTP error responses using this method. check these posts.

I am developing a similar workaround using the method described in this other post.

Will update with the details soon. Good luck!

Upvotes: 3

Related Questions