Android WebView - 1st LoadData() works fine, subsequent calls do not update display

坚强是说给别人听的谎言 提交于 2019-12-03 04:56:35
xtera

Use

webview.loadDataWithBaseURL("same://ur/l/tat/does/not/work", "data", "text/html", "utf-8", null);

it works fine. loaddata does not refresh next time the data is loaded.

For some reason you have to clear the content first. The "load..." methods don't seem to be explicitly appending their content but it doesn't work. I think it used to be WebView.clearView() but that was deprecated. The doc for the deprecated method on the Android site actually tells you to use WebView.loadUrl("about:blank") as a replacement for that method. So...

WebView.loadUrl("about:blank");
WebView.loadData(data, mime, encoding);

...does the trick for me. It seems a little dirty but I wouldn't dare disobey Google! I'm not sure if anyone else is doing this but I am just loading a String in that I had read from an "asset." I'm using it to display help docs. So I'm not using any actual URL's; I'm just using the WebView as an HTML renderer.

Note: For those newbies out there (like me only about a month ago) make sure to replace "WebView" with an instance of your variable. These are not static methods.

Such approach will work

webView.loadDataWithBaseURL("fake-url", "<html></html>", "text/html", "UTF-8", null);
webView.loadData(htmlBuilder.toString(), "text/html", "UTF-8");

Those who are still having the issue i found a quick solution just use a handler for this

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            webView.loadDataWithBaseURL("", html, "text/html", "UTF-8", null);
        }
    }, 10) ;

You need to loadDataWithBaseURL in main thread

I was able to make the browser refresh on each update by giving the html document a different id each time: please see below at // WEBVIEW.

package com.example.scroll;
// philip r brenan at gmail.com, www.appaapps.com 
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;

public class MainActivity extends Activity
 {protected void onCreate(Bundle savedInstanceState)
   {super.onCreate(savedInstanceState);
    setContentView(new MyWebView(this)); 
   }
  class MyWebView extends WebView 
   {MyWebView(Context Context)
     {super(Context);
      getSettings().setJavaScriptEnabled(true);
      addJavascriptInterface(this, "Android");   
      new Thread()
       {public void run()
         {for(int j = 0; j < 100; ++j)
           {post(new Runnable()
             {public void run()
               {loadData(content(), "text/html", "utf-8"); // Display in browser
               }
             });    
            try {Thread.sleep(5000);} catch(Exception e) {}
           }  
         }
       }.start();
     } 
    int c = 0, C = 1;
    String content() 
     {final StringBuilder s = new StringBuilder();
      //s.append("<html id="+(C++)+"><body>"); // WEBVIEW REFRESHES CORRECTLY *************** 
      s.append("<html><body>");              // WEBVIEW DOES NOT REFRESH ******************

      s.append("<h1 id=11>1111</h1>");
      s.append("<script>location.href = '#22';</script>");
      for(int i = 0; i < 10; ++i) s.append("<p>"+c+c+c); ++c;

      s.append("<h1 id=22>2222</h1>");
      for(int i = 0; i < 10; ++i) s.append("<p>"+c+c+c); ++c;
      Log.e("AAAAAA", "content="+s.toString());
      s.append("</body></html>");
      return s.toString();
     }
   } 
 } 
String urlUnique = String.format("http://%s", java.util.UUID.randomUUID().toString());
                    webView.loadDataWithBaseURL(urlUnique, "<html></html>", "text/html", "UTF-8", null);
                    Thread.sleep(200);
                    webView.loadData(htmlData, "text/html", "UTF-8");
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!