webView 的onReceivedError();

以前判断没有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。
页面。因此,建议在这个回调中执行最低要求的工作。

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


版权声明:本文为qq_33413264原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。