WebViewClient.onPageStarted( ) called twice when specifying non-existent URL via WebView.loadURL()

Dr. Benedict Porkins picture Dr. Benedict Porkins · Mar 29, 2011 · Viewed 18.1k times · Source

Here is my code

public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    WebView webView = (WebView)findViewById(R.id.webView);

    // Assign webclient.
    webView.setWebViewClient(new WebViewClient( ) {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            Log.d("TAG", url);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Log.d("TAG", "failed: " + failingUrl + ", error code: " + errorCode + " [" + description + "]");
        }
    });


    webView.loadUrl("http://m.vooglemoogle.com" );
}



}

Results in the following log:

03-29 13:40:27.005: DEBUG/TAG(10948): http://m.vooglemoogle.com/
03-29 13:40:27.599: DEBUG/TAG(10948): failed: http://m.vooglemoogle.com/, error code: -2[The URL could not be found.]
03-29 13:40:27.607: DEBUG/TAG(10948): http://m.vooglemoogle.com/

Note another call to onPageStarted( ) ... Does anyone know the reason behind this? cheers!

Answer

THelper picture THelper · Dec 9, 2011

I encountered the same problem while testing my app on an AVD with API 7 (not sure if this is relevant but in any case).

I noticed that the exact sequence of callbacks is the following:

onPageStarted()     // url = non-existing url
onLoadResource()    // url = non-existing url
onReceivedError()   // url = non-existing url

onPageStarted()     // url = non-existing url
onLoadResource()    // url = file://android_assed/webkit/android-weberror.png
onPageFinished()    // url = non-existing url

So I guess the loading of the Android "Web page not available" page is triggering the second onPageStarted call.