【问题标题】:'Webpage not available' with WebView.loadData() ONLY in emulator仅在模拟器中使用 WebView.loadData() 的“网页不可用”
【发布时间】:2012-01-15 08:08:45
【问题描述】:

我在WebView 上调用loadData 并以字符串的形式向其传递一些HTML,如下所示:

webView.loadData( htmlString, "text/html", "utf-8" );

它在我的 Galaxy Tab 10.1 上运行良好,但 WebView 显示:

Webpage not available

在模拟器上运行时,所有设置都与我的 Galaxy Tab 匹配。在清单中设置android.permission.INTERNET 无效,但我不需要该权限,因为我正在呈现内存中的 HTML,并且不通过数据连接访问任何内容。

发生了什么事?

【问题讨论】:

    标签: android android-emulator webview emulation


    【解决方案1】:

    试试这个代码

    webView.loadData( URLEncoder.encode(htmlString).replaceAll("\\+"," "), "text/html", "utf-8" );
    

    插入

    webView.loadData( htmlString, "text/html", "utf-8" );
    

    它应该可以工作,因为有时像 '%'、'\'、'#' 这样的字符如果编码不正确会产生问题

    【讨论】:

    • 谢谢 Viraj,你刚刚救了我的命,我被这个问题困扰了很久。为你 +1(如果可以的话,我会给你 +100)。
    • 你的回答对我有帮助,为你 +1 :)
    • 也帮助了我,谢谢 Viraj。也停留在这个问题上。为你 +1。
    【解决方案2】:

    2.x 平台上,loadData() 在某些情况下会失败(它需要转义 html),请改用loadDataWithBaseURL(),并为baseUrlhistoryUrl 传递null

    webView.loadDataWithBaseURL(null, htmlString, "text/html", "utf-8", null);
    

    【讨论】:

    • 这对我有用。它可能比“使用 URLEncoder 和 replaceAll 正则表达式”更快。
    • 为我+1。只是一个快速的附录:您应该编辑您的答案并输入webview.loadDataWithBaseUrl(null, htmlString, "text/html", "utf-8", null); 感谢您的帮助!
    • thnx 很多 fardjad,我可能是你 40 年的奴隶
    • loadDataWithBaseUrl() 一直有效,而 loadData() 有时会产生“网页错误”。
    • 我对此赞不绝口......直到现在我才知道为什么这个功能很有用。
    【解决方案3】:

    实际上感谢@Viraj 的回答。

    Google 目前已弃用上述内容,您应该改用它:

    webView.loadData(URLEncoder.encode(mAdvertisement.getContent(), "UTF-8").replaceAll("\\+", " "), "text/html", "UTF-8");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-27
      • 1970-01-01
      • 2017-09-18
      • 1970-01-01
      • 2010-12-27
      相关资源
      最近更新 更多