Android 监听WebView加载失败

javascript 复制代码
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);
            }
        }
    }

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

javascript 复制代码
/** 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 小时前
Escrcpy 3.0投屏控制软件使用教程:无线/有线连接+虚拟显示功能等
android
东京老树根4 小时前
Android - 用Scrcpy 将手机投屏到Windows电脑上
android
Wgllss5 小时前
完整烟花效果,Compose + 协程 + Flow + Channel 轻松实现
android·架构·android jetpack
扛麻袋的少年5 小时前
6.Kotlin的Duration类
android·开发语言·kotlin
独自破碎E5 小时前
得物25年春招-安卓部分笔试题1
android
雨白6 小时前
Android 自定义 View:精通文字的测量与高级排版
android
Jasonakeke6 小时前
【重学MySQL】八十八、8.0版本核心新特性全解析
android·数据库·mysql
一条上岸小咸鱼8 小时前
Kotlin 类型检查与转换
android·kotlin
闲暇部落9 小时前
android studio配置 build
android·android studio·build
_祝你今天愉快10 小时前
Android FrameWork - Zygote 启动流程分析
android