WebView自定义原生错误页

先了解WebView  setWebViewClient 和 setWebViewClient的调用顺序:

网络正常情况下:

11-09 11:24:29.012 29617-29617/me.haotv.zhibo D/FeedADFragment___: getCpuInfoUrl url = https://cpu.baidu.com/1022/b3883e06
11-09 11:24:29.038 29617-29617/me.haotv.zhibo D/FeedADFragment___: getCpuInfoUrl url = https://cpu.baidu.com/1024/b3883e06
11-09 11:24:29.043 29617-29617/me.haotv.zhibo D/FeedADFragment___: setWebChromeClient onProgressChanged:------------10
11-09 11:24:29.043 29617-29617/me.haotv.zhibo D/FeedADFragment___: setWebChromeClient onProgressChanged:------------10
11-09 11:24:29.112 29617-29617/me.haotv.zhibo D/FeedADFragment___: setWebViewClient onPageStarted: ------->url = https://cpu.baidu.com/1022/b3883e06
11-09 11:24:29.112 29617-29617/me.haotv.zhibo D/FeedADFragment___: setWebViewClient onPageStarted: ------->url = https://cpu.baidu.com/1024/b3883e06
11-09 11:24:29.203 29617-29617/me.haotv.zhibo D/FeedADFragment___: setWebChromeClient onProgressChanged:------------10
11-09 11:24:29.212 29617-29617/me.haotv.zhibo D/FeedADFragment___: setWebChromeClient onProgressChanged:------------10
11-09 11:24:29.580 29617-29617/me.haotv.zhibo D/FeedADFragment___: setWebChromeClient onProgressChanged:------------20
11-09 11:24:29.590 29617-29617/me.haotv.zhibo D/FeedADFragment___: setWebChromeClient onReceivedTitle:------------
11-09 11:24:29.757 29617-29617/me.haotv.zhibo D/FeedADFragment___: setWebChromeClient onProgressChanged:------------23
11-09 11:24:29.779 29617-29617/me.haotv.zhibo D/FeedADFragment___: setWebChromeClient onProgressChanged:------------20
11-09 11:24:29.903 29617-29617/me.haotv.zhibo D/FeedADFragment___: setWebChromeClient onReceivedTitle:------------
11-09 11:24:29.915 29617-29617/me.haotv.zhibo D/FeedADFragment___: setWebChromeClient onProgressChanged:------------24
11-09 11:24:29.916 29617-29617/me.haotv.zhibo D/FeedADFragment___: setWebChromeClient onProgressChanged:------------70
11-09 11:24:31.221 29617-29617/me.haotv.zhibo D/FeedADFragment___: setWebChromeClient onProgressChanged:------------42
11-09 11:24:31.781 29617-29617/me.haotv.zhibo D/FeedADFragment___: setWebChromeClient onProgressChanged:------------43
11-09 11:24:31.873 29617-29617/me.haotv.zhibo D/FeedADFragment___: setWebChromeClient onProgressChanged:------------90
11-09 11:24:31.883 29617-29617/me.haotv.zhibo D/FeedADFragment___: setWebChromeClient onProgressChanged:------------65
11-09 11:24:32.076 29617-29617/me.haotv.zhibo D/FeedADFragment___: setWebChromeClient onProgressChanged:------------65
11-09 11:24:32.113 29617-29617/me.haotv.zhibo D/FeedADFragment___: setWebChromeClient onProgressChanged:------------100
11-09 11:24:32.113 29617-29617/me.haotv.zhibo D/FeedADFragment___: setWebChromeClient onProgressChanged:------------100
11-09 11:24:32.114 29617-29617/me.haotv.zhibo D/FeedADFragment___: setWebViewClient onPageFinished: ------->url
11-09 11:24:32.859 29617-29617/me.haotv.zhibo D/FeedADFragment___: setWebChromeClient onProgressChanged:------------89
11-09 11:24:32.874 29617-29617/me.haotv.zhibo D/FeedADFragment___: setWebChromeClient onProgressChanged:------------100
11-09 11:24:32.875 29617-29617/me.haotv.zhibo D/FeedADFragment___: setWebChromeClient onProgressChanged:------------100
11-09 11:24:32.875 29617-29617/me.haotv.zhibo D/FeedADFragment___: setWebViewClient onPageFinished: ------->url

网络异常情况下

11-08 18:01:55.823 11150-11150/me.haotv.zhibo D/FeedADFragment___: getCpuInfoUrl url = https://cpu.baidu.com/1032/b3883e06
11-08 18:01:55.832 11150-11150/me.haotv.zhibo D/FeedADFragment___: setWebChromeClient onProgressChanged:------------10
11-08 18:01:55.840 11150-11150/me.haotv.zhibo D/FeedADFragment___: setWebViewClient onPageStarted: ------->url
11-08 18:01:55.866 11150-11150/me.haotv.zhibo D/FeedADFragment___: setWebViewClient onReceivedError: 
11-08 18:01:55.867 11150-11150/me.haotv.zhibo D/FeedADFragment___: setWebViewClient onPageFinished: ------->url
11-08 18:01:55.898 11150-11150/me.haotv.zhibo D/FeedADFragment___: setWebChromeClient onProgressChanged:------------100
11-08 18:01:55.899 11150-11150/me.haotv.zhibo D/FeedADFragment___: setWebChromeClient onProgressChanged:------------100
11-08 18:01:55.899 11150-11150/me.haotv.zhibo D/FeedADFragment___: setWebViewClient onPageFinished: ------->url

故可在onPageStart的时候显示Loading页面,隐藏错误页面 , 一旦收到onReceivedError(注意6.0和低版本调用的方法不同, 都写上) 就显示自定义的原生错误页面.

注意:  setWebChromeClient onReceivedTitle 这个函数执行的很早, 在Progress 10%的时候就执行了.

当然你可以在这里显示标题,虽然页面还在laoding... 

如果此时点击自定义错误页面的按钮刷新, 由于onPageStarted回调导致了Webview的那个错误页面显示出来, 然后再显示我们自己的自定义错误页面, 体验也不好, 那就在onReceiveError的时候load一个空白页面,

最后最后请注意, 如果当前没网络, 别webview.loadurl 

总代码如下

package me.haotv.zhibo.fragment;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.baidu.mobads.CpuInfoManager;

import me.haotv.zhibo.R;
import me.haotv.zhibo.ad.ADWebActivity;
import me.haotv.zhibo.fragment.base.BaseFragment;
import me.haotv.zhibo.utils.DeviceUtil;
import me.haotv.zhibo.utils.LogUtil;
import me.haotv.zhibo.view.MultiStateView;

/**
 * Created by Administrator on 2017/8/22 0022.
 */

public class FeedADFragment extends BaseFragment {

    private static final String TAG = "FeedADFragment___";
    private WebView mFeed_ad_webview;
    private int CHANNEL_MIX = 1032;
    private MultiStateView mStateView;
    private View mEmptyView;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_feed_ad;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    @Override
    protected void initViews(View view) {
        mStateView = (MultiStateView) view.findViewById(R.id.feed_ad_container);
        mEmptyView = mStateView.getView(MultiStateView.ViewState.EMPTY).findViewById(R.id.empty_view);
        mFeed_ad_webview = (WebView) findViewById(R.id.feed_ad_wv);
        configWebView(mFeed_ad_webview);
    }


    private void configWebView(final WebView webView) {
        webView.setVerticalScrollBarEnabled(false);
        webView.setHorizontalScrollBarEnabled(false);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);

        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                super.onProgressChanged(view, progress);
                LogUtil.d(TAG, "setWebChromeClient onProgressChanged:------------" + progress);
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                LogUtil.d(TAG, "setWebChromeClient onReceivedTitle:------------");
                super.onReceivedTitle(view, title);
            }

            @Override
            public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
                LogUtil.d(TAG, "onCreateWindow ------------");
                return super.onCreateWindow(view, isDialog, isUserGesture, resultMsg);
            }

        });
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                LogUtil.d(TAG, "setWebViewClient onPageStarted: ------->url = " + url);
                super.onPageStarted(view, url, favicon);
                showContentPage();
                if (TextUtils.isEmpty(url)) {
                    LogUtil.e(TAG, "url isEmpty url = " + url);
                    return;
                }
                if (url.contains("cpu.baidu.com")) {
                    if (url.contains("detail")) {//CPU的detail页面要跳转
                        view.stopLoading();
                        ADWebActivity.startFrom(FeedADFragment.this.getActivity(), url);
                    } else {
                        //do nothing, 不需要拦截
                    }
                }
                if (!url.contains("cpu.baidu.com")) {//非cpu的网址, 直接跳转
                    view.stopLoading();
                    ADWebActivity.startFrom(FeedADFragment.this.getActivity(), url);
                }

            }

            @Override
            public void onPageFinished(WebView view, String url) {
                LogUtil.d(TAG, "setWebViewClient onPageFinished: ------->url");
                super.onPageFinished(view, url);
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                //6.0以下执行
                LogUtil.d(TAG, "setWebViewClient onReceivedError: ------->errorCode" + errorCode + ":" + description);
                view.loadUrl("about:blank");
                showErrorPage();
            }

            //处理网页加载失败时
            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                //6.0以上执行
                LogUtil.d(TAG, "setWebViewClient onReceivedError: ");
                view.loadUrl("about:blank");
                showErrorPage();
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                LogUtil.d(TAG, "shouldOverrideUrlLoading url " + url);
                if (TextUtils.isEmpty(url)) {
                    return true;
                }
                view.loadUrl(url);
                return true;
            }
        });

    }

    @Override
    protected void setListeners() {
        mEmptyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestDataFromNet();
            }
        });
    }

    @Override
    protected void requestDataFromNet() {
        if (DeviceUtil.isNetworkAvailable()) {
            CpuInfoManager.getCpuInfoUrl(getActivity(), getAppsid(), getValue(), new CpuInfoManager.UrlListener() {

                @Override
                public void onUrl(String url) {
                    LogUtil.d(TAG, "getCpuInfoUrl url = " + url);
                    mFeed_ad_webview.loadUrl(url);
//                mFeed_ad_webview.loadUrl("http://www.qq.com");
                }
            });
        } else {
            LogUtil.e(TAG, "Network available, will not load cpu AD");
            showErrorPage();
        }

    }


    public String getAppsid() {
        String appid = DeviceUtil.getAndroidManifestMata("BaiduMobAd_APP_ID");
        LogUtil.d(TAG, "appid = " + appid);
        return appid;
    }

    public int getValue() {
        return CHANNEL_MIX;
    }

    public void showErrorPage() {
        if (mStateView.getViewState() != MultiStateView.ViewState.EMPTY) {
            mStateView.setViewState(MultiStateView.ViewState.EMPTY);
        }
    }

    public void showContentPage() {
        if (mStateView.getViewState() != MultiStateView.ViewState.CONTENT) {
            mStateView.setViewState(MultiStateView.ViewState.CONTENT);
        }
    }
}


 

转载于:https://my.oschina.net/sfshine/blog/1570056

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值