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;
相关推荐
冬奇Lab19 分钟前
Android触摸事件分发、手势识别与输入优化实战
android·源码阅读
城东米粉儿3 小时前
Android MediaPlayer 笔记
android
Jony_4 小时前
Android 启动优化方案
android
阿巴斯甜4 小时前
Android studio 报错:Cause: error=86, Bad CPU type in executable
android
张小潇4 小时前
AOSP15 Input专题InputReader源码分析
android
_小马快跑_8 小时前
Kotlin | 协程调度器选择:何时用CoroutineScope配置,何时用launch指定?
android
_小马快跑_8 小时前
Kotlin | 从SparseArray、ArrayMap的set操作符看类型检查的不同
android
_小马快跑_8 小时前
Android | 为什么有了ArrayMap还要再设计SparseArray?
android
_小马快跑_8 小时前
Android TextView图标对齐优化:使用LayerList精准控制drawable位置
android
_小马快跑_8 小时前
Kotlin协程并发控制:多线程环境下的顺序执行
android