错误在最近的Android的WebView

问题描述 投票:0回答:1

使用的WebView呈现应用的内容时,没有任何人有类似的问题?这似乎是“#”是不是在最近更新运作良好。

public class MainActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    WebView webView = findViewById(R.id.webView);
    String html = "<html><body>This is a #test</body></html>";
    webView.loadData(html, "text/html", null);
 }
}

显示“测试是”(没有测试)在最近更新的设备。如果您在模拟器中运行这个,你可能看不出问题。

仿真器:

enter image description here

设备:

enter image description here

android android-webview
1个回答
2
投票

我找到了解决办法。从文档它说...

的数据是BASE64或URL编码

所以,HTML不能只是一个简单的字符串。它应该像这样的base64编码

WebView webView = findViewById(R.id.webView);
String html = "<html><body>This is a #test</body></html>";
String base64 = Base64.encodeToString(html.getBytes(), Base64.NO_PADDING);
webView.loadData(base64, "text/html", "base64");

然后,它工作正常。它不使用在早期的Chrome版本编码做工精细。

© www.soinside.com 2019 - 2024. All rights reserved.