0

I have a following scenario for 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;
    }
}

// this is log cat for point 3 below

 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

Ok,

  1. When network is not available I ran this code, webviewclient's onReceivedError get called,
  2. If network available and page loading started, and network disconnect, then also onReceivedError get called.
  3. But there is a some instance of time,(count 1,2,3 after pressing button and disconnect network with f8 on emulator) mWebView.loadUrl(url) gets called, webViewclient's methods gets called like below

and WebView displays blank white screen, onReceivedError not called. here is logcat

and

  1. What is wrong with this code.
  2. Why onReceivedError not called. If it does not get called, how to handle this situation
  3. How to know webView has not loaded anyting and finish activity
Babasaheb
  • 683
  • 6
  • 20

1 Answers1

0

it will certainly call page started and page finished because onReceivedError will be called on:-

-click of any link (if internet not present) then should override url will throw an error which it will handle
-on activity started or when webview is called

for all the exceptions which are not handled by onRecievedError you have to check http status

maddy
  • 216
  • 4
  • 17
  • thanks for your response, but is there any way to check status in webView, as far I searched, didn't get any method/solution for that. – Babasaheb Mar 08 '13 at 10:48
  • yea you have to check http status and refer to this and read both of them you will get your solution http://en.wikipedia.org/wiki/List_of_HTTP_status_codes http://stackoverflow.com/questions/11889020/get-http-status-code-in-android-webview – maddy Mar 08 '13 at 11:24
  • For now I have added Internet check in onPageFinished(..) and called finishActivity. – Babasaheb Mar 14 '13 at 15:41
  • thats you wish but if internet disconnects while page is loading then? – maddy Mar 15 '13 at 05:21