网络断开后,Android WebView为空白

时间:2013-03-07 07:53:18

标签: android webview webviewclient

我有一个关于webview的以下场景

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view_tutorial);
        mWebView = (WebView) findViewById(R.id.webView1);

        mWebView.setWebViewClient(mWebViewClient);

        mWebView.setInitialScale(0);
        mWebView.setVerticalScrollBarEnabled(false);
        mWebView.requestFocusFromTouch();

        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);

        Button btnGo = (Button) findViewById(R.id.button1);
        btnGo.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                String url = "http://www.google.com";// here goes my server url with https://...... for authentication
                showProgressDialog("Loading");
                mWebView.loadUrl(url);
            }
        });


}    
private void showProgressDialog(String title) {

        if(mProgress == null || !mProgress.isShowing()){
            mProgress = ProgressDialog.show(this, title, "Please wait...", true,
                    true, new OnCancelListener() {
                public void onCancel(DialogInterface pd) {
                    finishActivity();
                }
            });
            mProgress.setCanceledOnTouchOutside(false);
            mProgress.setCancelable(true);  
        }       
    }
private void finishActivity() {
    if(mWebView!=null){
        finish();   
    }

}
private final WebViewClient mWebViewClient = new WebViewClient() {



        public void onPageStarted(WebView view, String url,
                android.graphics.Bitmap favicon) {

            super.onPageStarted(view, url, favicon);
            Log.d("test", "page started");

        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.d("test", "page should override called");
                return super.shouldOverrideUrlLoading(view, url);
        }

        @Override
        public void onPageFinished(WebView view, String url) {

            if (mIsLoadingSigninPage) {
                    mIsLoadingSigninPage = false;
                dismissProgressDialog();
            }
            Log.d("test", "page finished");
            super.onPageFinished(view, url);
        }

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

            mIsLoadingSigninPage = false;
            dismissProgressDialog();
            Log.d("test", "page error received");


            super.onReceivedError(view, errorCode, description, failingUrl);
            finishActivity();
        }
};
private void dismissProgressDialog() {
    if (mProgress != null && mProgress.isShowing()) {
        mProgress.dismiss();
        mProgress =null;
    }
}

//这是下面第3点的log cat

 03-07 07:31:20.977: D/test(1431): page started
03-07 07:31:22.788: D/test(1431): page should override called
03-07 07:31:22.887: D/test(1431): page started
03-07 07:31:24.496: D/test(1431): page finished

确定,

  1. 当网络不可用时,我运行了此代码,调用了webviewclient的onReceivedError,
  2. 如果网络可用且页面加载已启动,并且网络断开连接,则还会调用onReceivedError。
  3. 但是有一些时间实例,(按下按钮后计数1,2,3并在模拟器上用f8断开网络)mWebView.loadUrl(url)被调用,webViewclient的方法被调用如下
  4. 并且WebView显示空白屏幕,未调用onReceivedError。 这是logcat

    1. 此代码有什么问题。
    2. 为什么没有调用onReceivedError。如果没有被调用,如何处理这种情况
    3. 如何知道webView未加载任何内容并完成活动

1 个答案:

答案 0 :(得分:0)

它肯定会调用页面启动和页面完成,因为将调用onReceivedError: -

- 点击任何链接(如果互联网不存在)然后应该覆盖url将抛出一个错误,它将处理
-on活动开始或调用webview时

对于onRecievedError未处理的所有异常,您必须检查http状态