android访问网页失败提示错误,WebView中加载页面错误处理

github地址(完整Demo下载,欢迎访问)

在Android项目中,我们通常会用WebView来加载网页面,当我们的手机没有联网,或是服务端不小心瘫痪的时候,WebView展示的效果就比较丑。

860e5658b54b

Paste_Image.png

解决办法

我们自定义一个View用来展示加载失败的样子,或者我们也可以直接写一个错误展示的HTML让WebView加载等等。我们进行重写的方法是WebViewClient的onReceivedError()方法。

效果图

860e5658b54b

自定义加载网页错误的view.jpg

MainActivity:

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";

private WebView webView;

private FrameLayout loadingLayout; //提示用户正在加载数据

private RelativeLayout webParentView;

private View mErrorView; //加载错误的视图

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

webView = (WebView) findViewById(R.id.webView);

loadingLayout = (FrameLayout) findViewById(R.id.loading_layout);

initErrorPage();//初始化自定义页面

initWebView();

}

private void initWebView() {

//加载需要显示的网页

webView.loadUrl("http://www.baidu.com/");

WebSettings mWebSettings = webView.getSettings();

mWebSettings.setJavaScriptEnabled(true); //允许加载javascript

mWebSettings.setSupportZoom(true); //允许缩放

mWebSettings.setBuiltInZoomControls(true); //原网页基础上缩放

mWebSettings.setUseWideViewPort(true); //任意比例缩放

webView.setWebViewClient(new WebViewClient() {

@Override

public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

super.onReceivedError(view, errorCode, description, failingUrl);

//6.0以下执行

Log.i(TAG, "onReceivedError: ------->errorCode" + errorCode + ":" + description);

//网络未连接

showErrorPage();

}

//处理网页加载失败时

@Override

public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {

super.onReceivedError(view, request, error);

//6.0以上执行

Log.i(TAG, "onReceivedError: ");

showErrorPage();//显示错误页面

}

});

webView.setWebChromeClient(new WebChromeClient() {

@Override

public void onProgressChanged(WebView view, int newProgress) {

Log.i(TAG, "onProgressChanged:----------->" + newProgress);

if (newProgress == 100) {

loadingLayout.setVisibility(View.GONE);

}

}

@Override

public void onReceivedTitle(WebView view, String title) {

super.onReceivedTitle(view, title);

Log.i(TAG, "onReceivedTitle:title ------>" + title);

if (title.contains("404")){

showErrorPage();

}

}

});

webParentView = (RelativeLayout) webView.getParent(); //获取父容器

}

/**

* 显示自定义错误提示页面,用一个View覆盖在WebView

*/

private void showErrorPage() {

webParentView.removeAllViews(); //移除加载网页错误时,默认的提示信息

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

webParentView.addView(mErrorView, 0, layoutParams); //添加自定义的错误提示的View

}

/***

* 显示加载失败时自定义的网页

*/

private void initErrorPage() {

if (mErrorView == null) {

mErrorView = View.inflate(this, R.layout.layout_load_error, null);

}

}

}

activity_main.xml

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="com.zx.errowebviewdemo.MainActivity">

android:id="@+id/webView"

android:layout_width="match_parent"

android:layout_height="match_parent" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

layout="@layout/loading_anim"

android:layout_centerInParent="true"/>

layout_load_error.xml

android:id="@+id/load_error_layout"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/error_iv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:contentDescription="@null"

android:src="@mipmap/no_data" />

android:id="@+id/reload_tv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/error_iv"

android:layout_centerHorizontal="true"

android:layout_marginTop="8dp"

android:gravity="center"

android:text="数据获取失败\n, 请检查网络后再重试 ~~~" />

layout/loading_anim.xml

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/loading_layout"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:contentDescription="@null"

android:scaleType="centerCrop"

android:src="@drawable/beijing" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:contentDescription="@null"

android:scaleType="centerCrop"

android:src="@drawable/loading_anim" />

android:id="@+id/loading_tv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="bottom|center_horizontal"

android:layout_marginBottom="3dp"

android:gravity="center"

android:text="@string/please_wait"

android:textColor="#ffffff" />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值