如果拦截WebView的错误和OS升级到4.4后链接不能点击的问题
android OS升级到4.4之后,有些WebView的链接我们点击无效了,下面可以解决其中的某一种情况:
webviewClient的shouldOverrideUrlLoading方法必须返回false,返回true会导致这个问题
有时候PM会有一种需求,如果不是Server那边的问题,那么想在界面上显示自定义的错误(如遇到连接到WIFI CMCC而没有移动的密码,手机显示着连接上了WIFI)
这时我们需要去截获WebView的error,可以通过重载setWebViewClient的方法去截获,可以参考google文档:点击这里打开
在代码:
<span style="white-space:pre"> </span>this.mWebView.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url.startsWith(ReportActivity.this.getString(R.string.url_start_chars1)) || url.startsWith(ReportActivity.this.getString(R.string.url_start_chars2))) { view.loadUrl(url); } else if(url.startsWith(ReportActivity.this.getString(R.string.url_start_chars3))){ Uri uri=Uri.parse(url); Intent emailIntent=new Intent(Intent.ACTION_SENDTO, uri); List<ResolveInfo> availableSoft = getPackageManager().queryIntentActivities(emailIntent, PackageManager.MATCH_DEFAULT_ONLY); if (availableSoft.size() > 0) { try { startActivity(emailIntent); } catch (Exception e) { } } } return false; } @Override public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { handler.proceed(); } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { //这里截获错误 isLoadingError = true; Toast.makeText(ReportActivity.this, "errorCode="+errorCode, Toast.LENGTH_LONG).show(); super.onReceivedError(view, errorCode, description, failingUrl); } }); this.mWebView.setWebChromeClient(new WebChromeClient() { @Override public void onProgressChanged(WebView view, int newProgress) { if (newProgress >= 80) { updateState(true); System.out.println("=========isSuccess onProgressChanged isLoadingError="+isLoadingError); if(isLoadingError) { mState = State.GetDataError; } updateStateView(); } } });
错误的类型有:
// These ints must match up to the hidden values in EventHandler. /** 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;
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。