其他分享
首页 > 其他分享> > Android WebViewClient回调调用过于频繁

Android WebViewClient回调调用过于频繁

作者:互联网

当我调用WebView#loadUrl时,我希望我只能获得一个WebViewClient#onPageFinished调用而没有WebViewClient#shouldOverrideUrlLoading调用.但是,我得到了一个WebViewClient#shouldOverrideUrlLoading(我通过始终调用WebView#loadUrl并返回true来实现),然后是两个具有相同URL的WebViewClient#onPageFinished调用.

我正在加载的页面使用了很多ajax请求. ajax请求是否会调用WebViewClient?我的页面中没有任何meta-refreshes.

这非常令人沮丧.难道我做错了什么?

解决方法:

这是a google groups discussion I started on the topic的交叉帖子.

我做了更多的研究,我想我理解正在发生的一切.

这是我的WebViewClient:

public class MyWebViewClient extends WebViewClient {
  @Override
  public void onPageStarted(WebView view, String url, Bitmap favicon) {
    System.out.println("onPageStarted: " + url);
  }

  @Override
  public boolean shouldOverrideUrlLoading(WebView webView, String url) {
    System.out.println("shouldOverrideUrlLoading: " + url);
    webView.loadUrl(url);
    return true;
  }

  @Override
  public void onPageFinished(WebView webView, String url) {
    System.out.println("onPageFinished: " + url);
  }
}

我的服务器有以下URL重定向:

http://example.com/resource -> http://example.com/user-name/resource
http://example.com/logout.jsp -> http://example.com/login/logout
http://example.com/login/logout -> http://example.com/login

这些URL是我无法控制的服务器的一部分,因此我只需处理该行为.

以下是加载http://example.com/resource的输出

onPageStarted: http://example.com/resource
onPageStarted: http://example.com/user-name/resource
shouldOverrideUrlLoading: http://example.com/user-name/resource
onPageFinished: hhttp://example.com/user-name/resource

此时,我的WebView有空白内容.我必须等到第二次onPageFinished后才能抓住我的内容.

onPageStarted: http://example.coms/user-name/resource
onPageFinished: http://example.com/user-name/resource

以下是加载http://example.com/logout.jsp的输出

onPageStarted: http://example.com/logout.jsp
onPageStarted: http://example.com/login/logout
shouldOverrideUrlLoading: http://example.com/login/logout
onPageFinished: http://example.com/login/logout
onPageStarted: http://example.com/login/logout
onPageStarted: http://example.com/login
shouldOverrideUrlLoading: http://example.com/login
onPageFinished: http://example.com/login

再次,此时,我的WebView有一个空白页面.我必须等到第3次onPageFinished才能从WebView获取我的内容.

onPageStarted: http://example.com/login
onPageFinished: http://example.com/login

根据文档,我不指望这种行为.请注意,onPageStarteds和onPageFinisheds的数量不平衡.我特别不喜欢用重定向的URL调用onPageFinished,但WebView不包含我的内容.我知道这种行为可能是不可改变的,但至少应记录这种意外行为.

标签:android,webview,webviewclient
来源: https://codeday.me/bug/20190713/1447879.html