4

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

我正在加载的页面使用了很多 ajax 请求。ajax 请求是否调用WebViewClient? 我的页面中没有任何元刷新。

这很令人沮丧。难道我做错了什么?

4

2 回答 2

12

这是我在该主题上开始的谷歌群组讨论的交叉帖子。

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

这是我的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

根据文档,我不希望出现这种行为。onPageStarted请注意, s 和s的数量不平衡onPageFinished。我特别不喜欢如何onPageFinished使用我的重定向 URL 进行调用,但WebView它不包含我的内容。我知道这种行为可能是不可改变的,但至少应该记录这种意外行为。

于 2011-03-05T19:40:56.087 回答
4
@Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
    System.out.println("shouldOverrideUrlLoading: " + url);
    return true;
}

您不需要显式调用webView.loadUrl(url)。通过覆盖返回 true 将自动让您现有的 webview 来处理新的页面加载。通过不必要地调用它,它会在原始周期(onPageStarted -> onPageFinished)的中间开始一个全新的网页加载周期在该 webView 上,因此总共触发了额外的回调周期。

于 2016-04-28T07:10:38.057 回答