• Android 监听WebView加载失败


    public class CustomWebViewClient extends WebViewClient {
     
            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    int errorCode = error.getErrorCode();
                    String errorMessage = error.getDescription().toString();
                    Log.i("CustomWebViewClient", "onReceivedError  errorCode : " + errorCode + " errorMessage : " + errorMessage);
                }
            }
     
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                    Log.i("CustomWebViewClient", "onReceivedError  errorCode : " + errorCode + " description : " + description);
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    注意: WebViewClient 的 onReceivedError()方法要根据安卓版本做版本兼容,android 6.0及以上回调上面的方法,如果是6.0以下,回调下面的函数。
    下面是errorCode的列举:

    /** Generic error */
        public static final int ERROR_UNKNOWN = -1;
        /** Server or proxy hostname lookup failed */
        public static final int ERROR_HOST_LOOKUP = -2;
        /** Unsupported authentication scheme (not basic or digest) */
        public static final int ERROR_UNSUPPORTED_AUTH_SCHEME = -3;
        /** User authentication failed on server */
        public static final int ERROR_AUTHENTICATION = -4;
        /** User authentication failed on proxy */
        public static final int ERROR_PROXY_AUTHENTICATION = -5;
        /** Failed to connect to the server */
        public static final int ERROR_CONNECT = -6;
        /** Failed to read or write to the server */
        public static final int ERROR_IO = -7;
        /** Connection timed out */
        public static final int ERROR_TIMEOUT = -8;
        /** Too many redirects */
        public static final int ERROR_REDIRECT_LOOP = -9;
        /** Unsupported URI scheme */
        public static final int ERROR_UNSUPPORTED_SCHEME = -10;
        /** Failed to perform SSL handshake */
        public static final int ERROR_FAILED_SSL_HANDSHAKE = -11;
        /** Malformed URL */
        public static final int ERROR_BAD_URL = -12;
        /** Generic file error */
        public static final int ERROR_FILE = -13;
        /** File not found */
        public static final int ERROR_FILE_NOT_FOUND = -14;
        /** Too many requests during this load */
        public static final int ERROR_TOO_MANY_REQUESTS = -15;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
  • 相关阅读:
    李沐动手学深度学习V2-bert和代码实现
    无涯教程-JavaScript - MROUND函数
    论文写作--总结
    【Unity Shader】自定义变体使用
    uniapp和vue组件之间的传值方法(父子传值,兄弟传值,跨级传值,vuex)
    jeecg-boot简单使用
    JavaSpringBoot的@Value设置默认值,用冒号:
    Vue实战篇三十五:实现滑动拼图验证登录
    uniapp全选功能制作
    媒体梦工厂AI智聊:轻松提升工作效率的智能助手
  • 原文地址:https://blog.csdn.net/weixin_42504805/article/details/132626769