直接访问URL时onPageFinished需要2分钟以上

时间:2013-07-19 02:02:10

标签: http webview android-webview webviewclient

在某些网站上,我注意到如果我直接访问内页 (即不是通过触摸同一网站上不同页面的链接),onPageFinished()将永远占用(即2分钟以上!)到达。

如果我通过触摸同一网站页面中的链接加载同一页面,onPageFinished()总是在1-2秒内到达(相同的互联网连接,相同的条件)。

在所有情况下,只有一个onPageStarted(),只有一个onPageFinished()。也就是说,不涉及重定向。

此外,在直接(慢)和从站内(快速)访问中,整个页面显示为完整(可视)。只有onPageStarted()由于某种原因拒绝到达(在直接访问中)。

为了更好地理解这个问题,我提供了这样一个页面的例子:

http://mobile.nytimes.com/2013/07/19/world/middleeast/touring-refugee-camp-kerry-sees-mounting-syrian-suffering.html?from=homepage

例如,如果您从网站的主页访问此页面,onPageFinished()的到达速度会快得多。

(主页本身,直接访问,在1-2秒内获得onPageFinished()!)

有什么可以解释这种行为?

如何解决此类问题?

更新1:查看LogCat输出我发现慢速(直接)情况的特征是在onPageStarted()之后立即爆发dalvikvm GC操作:

07-18 21:22:33.876: D/dalvikvm(6298): GC_FOR_MALLOC freed 10371 objects / 495744 bytes in 54ms
07-18 21:22:34.016: D/dalvikvm(6298): GC_FOR_MALLOC freed 808 objects / 50824 bytes in 51ms
07-18 21:22:34.586: D/dalvikvm(6298): GC_FOR_MALLOC freed 1092 objects / 297328 bytes in 72ms
07-18 21:22:34.646: D/dalvikvm(6298): GC_EXTERNAL_ALLOC freed 49 objects / 2296 bytes in 59ms
07-18 21:22:36.526: I/global(6298): Default buffer size used in BufferedInputStream constructor. It would be better to be explicit if an 8k buffer is required.
07-18 21:22:36.626: D/dalvikvm(6298): GC_FOR_MALLOC freed 4687 objects / 513240 bytes in 86ms
07-18 21:22:36.626: I/dalvikvm-heap(6298): Grow heap (frag case) to 3.304MB for 131088-byte allocation
07-18 21:22:36.706: D/dalvikvm(6298): GC_FOR_MALLOC freed 928 objects / 53008 bytes in 69ms
07-18 21:22:36.766: D/dalvikvm(6298): GC_FOR_MALLOC freed 9 objects / 264 bytes in 61ms
07-18 21:22:36.766: I/dalvikvm-heap(6298): Grow heap (frag case) to 3.378MB for 131088-byte allocation
07-18 21:22:36.836: D/dalvikvm(6298): GC_FOR_MALLOC freed 0 objects / 0 bytes in 69ms
07-18 21:22:37.286: D/dalvikvm(6298): GC_FOR_MALLOC freed 547 objects / 98160 bytes in 73ms
07-18 21:22:37.286: I/dalvikvm-heap(6298): Grow heap (frag case) to 3.617MB for 262480-byte allocation
07-18 21:22:37.336: D/dalvikvm(6298): GC_FOR_MALLOC freed 175 objects / 8384 bytes in 46ms
07-18 21:22:37.706: D/dalvikvm(6298): GC_FOR_MALLOC freed 340 objects / 183688 bytes in 78ms
07-18 21:22:37.706: I/dalvikvm-heap(6298): Grow heap (frag case) to 3.994MB for 527244-byte allocation
07-18 21:22:37.776: D/dalvikvm(6298): GC_FOR_MALLOC freed 104 objects / 4560 bytes in 69ms
07-18 21:22:38.006: D/dalvikvm(164): GC_EXPLICIT freed 21550 objects / 1176488 bytes in 137ms
07-18 21:22:38.286: D/dalvikvm(6298): GC_FOR_MALLOC freed 227 objects / 299888 bytes in 50ms
07-18 21:22:38.286: I/dalvikvm-heap(6298): Grow heap (frag case) to 4.135MB for 444555-byte allocation
07-18 21:22:38.326: D/dalvikvm(6298): GC_FOR_MALLOC freed 1 objects / 16 bytes in 41ms
07-18 21:22:38.376: D/dalvikvm(6298): GC_FOR_MALLOC freed 352 objects / 687432 bytes in 36ms
07-18 21:22:38.376: I/dalvikvm-heap(6298): Grow heap (frag case) to 4.330MB for 592732-byte allocation
07-18 21:22:38.416: D/dalvikvm(6298): GC_FOR_MALLOC freed 2 objects / 104 bytes in 44ms
07-18 21:22:38.456: D/dalvikvm(6298): GC_FOR_MALLOC freed 4 objects / 96 bytes in 33ms
07-18 21:22:38.456: I/dalvikvm-heap(6298): Grow heap (frag case) to 4.612MB for 296374-byte allocation
07-18 21:22:38.496: D/dalvikvm(6298): GC_FOR_MALLOC freed 0 objects / 0 bytes in 41ms
07-18 21:22:38.536: D/dalvikvm(6298): GC_FOR_MALLOC freed 162 objects / 599848 bytes in 29ms
07-18 21:22:38.536: I/dalvikvm-heap(6298): Grow heap (frag case) to 5.170MB for 1185448-byte allocation
07-18 21:22:38.576: D/dalvikvm(6298): GC_FOR_MALLOC freed 0 objects / 0 bytes in 40ms
07-18 21:22:38.626: D/dalvikvm(6298): GC_FOR_MALLOC freed 7 objects / 1185616 bytes in 35ms
07-18 21:22:38.626: I/dalvikvm-heap(6298): Grow heap (frag case) to 5.443MB for 878616-byte allocation
07-18 21:22:38.676: D/dalvikvm(6298): GC_FOR_MALLOC freed 0 objects / 0 bytes in 42ms

这是什么意思?这是网站的问题吗?还是在我的代码中?

更新2:不仅onPageFinished()因直接网址访问而延迟,而且还WebChromClient.onProgressChanged()!它总是冻结在89%的标记,然后在收到onPageFinished()后立即跳到100%。奇怪的事情正在发生。为什么呢?

这可能是网站的故意行为吗?

如果您想回答“是”,为什么在使用其他浏览器(例如Firefox或Chrome)直接访问同一页面时不会这样做?

如果不是特定网站的故意行为(即我的代码中的错误),为什么其他网站不会发生这种情况?

1 个答案:

答案 0 :(得分:0)

这应该是因为网站必须在内页中加载太多资源。

通过链接访问时加载速度更快的原因是,大多数网页资源都是共享的,并且可以通过浏览导航由webview缓存。