通过WebView.loadURL()指定不存在的URL时,WebViewClient.onPageStarted()调用两次

Dr.*_*ins 13 android android-webview webviewclient

这是我的代码

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" );
}



}
Run Code Online (Sandbox Code Playgroud)

结果如下:

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/
Run Code Online (Sandbox Code Playgroud)

注意另一个对onPageStarted()的调用...有谁知道这背后的原因?干杯!

THe*_*per 14

我在使用API​​ 7在AVD上测试我的应用程序时遇到了同样的问题(不确定这是否相关,但无论如何).

我注意到回调的确切顺序如下:

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
Run Code Online (Sandbox Code Playgroud)

所以我猜Android加载"网页不可用"页面会触发第二次onPageStarted调用.


小智 -1

在android API中,你可以找到注释:

通知主机应用程序页面已开始加载。每次主框架加载时都会调用此方法一次,因此具有 iframe 或框架集的页面将为主框架调用一次 onPageStarted。这也意味着当嵌入框架的内容发生更改(即单击目标为 iframe 的链接)时,不会调用 onPageStarted。

这表明它可能是由网页中的“iframe”引起的。