webView 的onReceivedError();

本文介绍了如何利用WebView的onReceivedError()方法来处理网页加载时的网络错误。当无法连接到服务器时,该方法会被调用。建议在此回调中进行基本的错误处理工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以前判断没有webview没有网的显示都是直接判断当前状态有木有网,然后没有网跳转到没有网的界面,今天发现webview比想象的强大,发现了这个方法onReceivedError();没有网的话会执行这个方法
直接上代码


布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
    >
    <TextView
        android:id="@+id/tv1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="升级维护中"
        android:visibility="gone"
        />
    <WebView
        android:id="@+id/activity_webview2"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        />


</LinearLayout>

代码

public class SecondActivity extends AppCompatActivity {
private WebView activity_webview;
    private TextView tv1;
    private String downLoadUrl = "https://www.baidu.com/";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        tv1=(TextView)findViewById(R.id.tv1);
        activity_webview=(WebView) findViewById(R.id.activity_webview2);
        activity_webview.loadUrl(downLoadUrl);
        activity_webview.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
            }
        });
        activity_webview.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }



            /**
             * Report web resource loading error to the host application. These errors usually indicate
             * inability to connect to the server. Note that unlike the deprecated version of the callback,
             * the new version will be called for any resource (iframe, image, etc), not just for the main
             * page. Thus, it is recommended to perform minimum required work in this callback.
             *
             * @param view    The WebView that is initiating the callback.
             * @param request The originating request.
             * @param error   Information about the error occured.
             */
            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                tv1.setVisibility(View.VISIBLE);
                activity_webview.setVisibility(View.GONE);
            }
        });
    }
}

这里写图片描述
这里写图片描述
效果就是这样的
onReceivedError,有两个不同构造的方法

这里写图片描述
这里写图片描述
第一个构造方法被废除了,我们尽量找没有被废除的,我们用第二个构造方法

/**
             * Report web resource loading error to the host application. These errors usually indicate
             * inability to connect to the server. Note that unlike the deprecated version of the callback,
             * the new version will be called for any resource (iframe, image, etc), not just for the main
             * page. Thus, it is recommended to perform minimum required work in this callback.
             *
             * @param view    The WebView that is initiating the callback.
             * @param request The originating request.
             * @param error   Information about the error occured.
             */

这段注释的意思是
向主机应用程序报告web资源加载错误。这些错误通常表明
无法连接到服务器。请注意,与回调的不赞成版本不同,
新版本将被调用任何资源(iframe、图像等),而不仅仅是针对main。
页面。因此,建议在这个回调中执行最低要求的工作。

就这么多吧,不早了睡了,遇到了以后早补充

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值