android - WebView偶尔无法加载,没有发起请求
高洛峰
高洛峰 2017-04-17 17:48:28
0
4
793

webview有时候加载不出网页,页面关掉再打开也不行,显示一片空白。放着不动,过一会,反而能打开网页。

当加载不出的时候,发现根本没有发起请求。

有大神知道原因吗?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(4)
阿神

WebView This control is a very important control in the Android system, and it will not cause the situation mentioned by the original poster. What the original poster may see is the surface display. WebView这个控件作为Android系统中很重要的一个控件,其不会出现楼主所说的情况的。楼主可能看到的是表面显示。

当调用WebViewloadUrl(String url)之后,WebView就开始加载网页了,但是在没有加载出来的时候,它会仍然显示着其原有的白色背景,加载出来后才会显示网页。这可能是你说的显示一片空白,然后过一会才能打开网页。其实一直都在加载,只是加载比较慢,你看不出来有变化而已。

楼主可以使用WebView.setWebViewClient,传入一个自定义的WebViewClient。同样还有WebView.setWebChromeClient。覆写其中一些函数:

webView.setWebViewClient(new WebViewClient() {

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        //页面开始加载
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        //页面加载完毕
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        //加载出现失败
        super.onReceivedError(view, errorCode, description, failingUrl);
    }

});
webView.setWebChromeClient(new WebChromeClient() {

    public void onProgressChanged(WebView view, int progress) {
        //加载过程回调,progress是接受到的数据的百分比
    }

});

楼主如果对WebView进行如上的配置,然后在loadUrl(String url),就能看到这些函数的回调了。问题里的内容都可以从Log

After calling WebView's loadUrl(String url), WebView starts loading the web page, but when it is not loaded, it will It still displays its original white background, and the web page will not be displayed until it is loaded. This may mean that the display goes blank and it takes a while before the web page can be opened. In fact, it is loading all the time, but it is loading slowly and you can't see any changes. 🎜 🎜The author can use WebView.setWebViewClient to pass in a customized WebViewClient. There is also WebView.setWebChromeClient. Override some of these functions: 🎜 rrreee 🎜If the poster configures WebView as above, and then in loadUrl(String url), you can see the callbacks of these functions. The content in the question can be answered from Log. 🎜
Peter_Zhu

1. I really encountered this situation yesterday
2. Analysis of the reasons: A. I suggest the poster use Charles to capture the packets

       B.我的原因:加载url的时候,一下子激增了ajax请求与图片请求
       C.我的解决方法:让前端的同学做一下分页加载以及懒加载,不要在打开url的瞬间发出那么多请求
巴扎黑

This is indeed the case, I have also encountered it, and found that if you do not call wv_viewerweb.removeAllViews();
// wv_viewerweb.destroy();
// wv_viewerweb = null; this problem will not occur

左手右手慢动作

Is the problem solved? ? ? I also fell into a trap, please give me a solution...

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template