gpt4 book ai didi

android - 使用 loadDataWithBaseURL 以 base64 编码加载数据

转载 作者:行者123 更新时间:2023-12-03 21:01:31 25 4
gpt4 key购买 nike

我有一个使用 WebView 的应用程序向用户显示一些本地 html。该应用程序在过去 2 年运行良好,但最近我收到太多关于该应用程序显示如下错误的报告:
"Web page not available The web page at data:text/html; charset=utf-8;charset=utf-8;base64, could not be loaded because: net::ERR_INVALID_RESPONSE"
显然,android 或 WebView 发生了一些变化,doc 确实发生了变化。是说:

Stricter UTF-8 decoder In Android 9, the UTF-8 decoder for Java language is stricter and follows the Unicode standard.



所以,我尝试用base64编码加载我的数据来解决这个问题。
String encodedHtml = Base64.encodeToString(resource.getBytes(), Base64.NO_WRAP);
loadDataWithBaseURL(basePath, encodedHtml, "text/html; charset=utf-8", "base64", null);

但是这个方法只打印原始的 base64 字符串。所以我尝试了这个:
loadData(encodedHtml, "text/html; charset=utf-8", "base64");
它显示html没有任何问题。

但是,我需要使用 loadDataWithBaseURL所以我可以发布处理html,例如。加载css、字体等。

那么 loadData 和有什么区别?和 loadDataWithBaseURL导致其中一个显示 html 而另一个显示原始 base64 字符串?

最佳答案

docs说(强调)

If the base URL uses the data scheme, this method is equivalent to calling loadData() and the historyUrl is ignored, and the data will be treated as part of a data: URL, including the requirement that the content be URL-encoded or base64 encoded. If the base URL uses any other scheme, then the data will be loaded into the WebView as a plain string (i.e. not part of a data URL) and any URL-encoded entities in the string will not be decoded.


这听起来好像没有办法提供像 https://example.com 这样的网站的基本 URL。 , 很遗憾。
我可以通过使用 html base tag 来解决这个问题。所以链接/图像/等中的相对URL。仍然可以工作。
String header = "<head> <base href='" + myBaseUrl + "'> </head>";
String encodedHtml = Base64.encodeToString((header + resource).getBytes(), Base64.NO_WRAP);
webview.loadData(encodedHtml, "text/html; charset=utf-8", "base64");

关于android - 使用 loadDataWithBaseURL 以 base64 编码加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57198560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com