Android WebView - 第一个 LoadData() 工作正常,后续调用不会更新显示

发布于 2024-10-01 03:03:45 字数 391 浏览 2 评论 0原文

第一次调用 LoadData() 后, onLoadResource 事件按其应有的方式触发,并且显示正常。接下来,我想用新页面刷新屏幕,当我第二次使用 LoadData() 时,页面不会更新,并且 onLoadResource() 不会触发。

然后第二次调用 LoadData() 只会触发 onPageFinished ... onPageStarted 永远不会触发!

解决方法是在 LoadData() 之后调用 .reload() ,但这会在活动中的其他逻辑期间导致各种问题。

为什么 LoadData() 不能多次工作?

我使用的是非常简单的 HTML,并且由于使用 .reload() 使其工作,所以我的 LoadData() 语句似乎不是问题。

任何想法都会有帮助,TIA

After the 1st call to LoadData() the event onLoadResource fires as it should and the display is fine. Next I want to refresh the screen with a new page, when I use LoadData() the second time the page does not update and onLoadResource() DOES NOT FIRE.

Then second call to LoadData() onlyfires onPageFinished ... onPageStarted never fires!

A work around was to call .reload() after LoadData() but that causes all sorts of problems during the other logic in the activity.

Why doesn't LoadData() work multiple times?

I am using extremely simple HTML, and since using .reload() makes it work my LoadData() statement doesn't seem to be the problem.

Any Ideas would be helpful, TIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

温柔女人霸气范 2024-10-08 03:03:45

使用

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

它效果很好。 loaddata 下次加载数据时不会刷新。

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.

纵山崖 2024-10-08 03:03:45

由于某种原因,您必须先清除内容。 “load...”方法似乎没有明确附加其内容,但它不起作用。我认为它曾经是 WebView.clearView() 但已被弃用。 Android site 上已弃用方法的文档实际上告诉您使用 WebView.loadUrl("about:blank") 来替代该方法。所以...

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

...对我有用。看起来有点肮脏,但我不敢违背谷歌!我不确定是否有其他人在这样做,但我只是加载一个我从“资产”中读取的字符串。我用它来显示帮助文档。所以我没有使用任何实际的 URL;我只是使用 WebView 作为 HTML 渲染器。

注意:对于那些新手(比如大约一个月前的我),请确保将“WebView”替换为变量的实例。这些不是静态方法。

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.

亣腦蒛氧 2024-10-08 03:03:45

这样的方法将会奏效

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

Such approach will work

webView.loadDataWithBaseURL("fake-url", "<html></html>", "text/html", "UTF-8", null);
webView.loadData(htmlBuilder.toString(), "text/html", "UTF-8");
惟欲睡 2024-10-08 03:03:45

对于仍然遇到问题的人,我找到了一个快速解决方案,只需使用处理程序即可

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

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) ;
凶凌 2024-10-08 03:03:45

您需要在主线程中加载DataWithBaseURL

You need to loadDataWithBaseURL in main thread

老娘不死你永远是小三 2024-10-08 03:03:45

我能够通过每次为 html 文档提供不同的 id 来使浏览器在每次更新时刷新:请参阅下面的 // 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();
     }
   } 
 } 

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();
     }
   } 
 } 
—━☆沉默づ 2024-10-08 03:03:45
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");
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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文