通过 WebView.loadURL() 指定不存在的 URL 时,WebViewClient.onPageStarted( ) 被调用两次
这是我的代码
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" );
}
}
结果如下日志:
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/
注意对 onPageStarted( ) 的另一个调用...有谁知道这背后的原因吗? 干杯!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在使用 API 7 的 AVD 上测试我的应用程序时遇到了同样的问题(不确定这是否相关,但无论如何)。
我注意到回调的确切顺序如下:
所以我猜测 Android“网页不可用”页面的加载正在触发第二个 onPageStarted 调用。
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:
So I guess the loading of the Android "Web page not available" page is triggering the second onPageStarted call.
我也曾为此苦苦挣扎,但我想我已经解决了。基本上,我设置了一个错误标志,以防止客户端处理更多回调。当我调用活动中的方法以再次尝试加载时,该标志会重置。这是我创建的要点中的一些示例代码 https://gist.github.com/museofwater/6373048
I struggled with this as well, but I think I've worked around it. Basically I set a flag on error to keep the client from processing any more callbacks. The flag gets reset when I call a method in the activity to try the load again. Here is some sample code from a gist I created https://gist.github.com/museofwater/6373048
在android API中,你可以找到注释:
这表明它可能是由网页中的“iframe”引起的。
In the android API, you can find the note:
Which suggests that it could be caused by "iframes" in the web page.