search

Home  >  Q&A  >  body text

android - WebView偶尔无法加载,没有发起请求

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

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

有大神知道原因吗?

高洛峰高洛峰2772 days ago790

reply all(4)I'll reply

  • 阿神

    阿神2017-04-17 17:50:28

    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. 🎜

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 17:50:28

    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的瞬间发出那么多请求

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 17:50:28

    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

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 17:50:28

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

    reply
    0
  • Cancelreply