这是来自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 有一个空白页。我必须等到第三个onPageFinished 才能从WebView 获取我的内容。
onPageStarted: http://example.com/login
onPageFinished: http://example.com/login
根据文档,我预计不会出现这种行为。请注意,onPageStarteds 和 onPageFinisheds 的数量不平衡。我特别不喜欢 onPageFinished 被重定向 URL 调用的方式,但 WebView 不包含我的内容。我知道这种行为可能是不可改变的,但至少应该记录这种意外行为。