【问题标题】:Android WebView not fully decoding StringAndroid WebView 没有完全解码字符串
【发布时间】:2025-12-17 05:35:01
【问题描述】:

我有一个非常简单的WebView,需要将文本显示为 HTML。我从byte 数组中的服务器检索数据。我需要将 byte 数组显示为可读文本。

var wv = FindViewById<WebView>(Resource.Id.webview);

var container = AutoFac.Container;
_routinesService = container.Resolve<IRoutinesService>();
byte[] documentHtml = _routinesService.GetFragment(documentId);

string mimeType = "text/html";
string encoding = "utf-8";
var html = System.Text.Encoding.UTF8.GetString(documentHtml);

wv.LoadDataWithBaseURL("", html, mimeType, encoding, "");

我尝试将byte数组转换为String,变量html变成\"U3RlcCAxOiBZb3UgYXJlIGEgd2lubmVyLg==\"

这在我看来像 Base64,当我使用在线转换器 (https://www.base64decode.org/) 时,它确实会转换为正确的文本。但是WebView 屏幕上的输出看起来就像"U3RlcCAxOiBZb3UgYXJlIGEgd2lubmVyLg=="

有什么我可能遗漏的想法吗?

只是为了了解更多信息,这是在 Xamarin.Android 中,因此代码是 C# 而不是 Java(但没有太大区别)。

【问题讨论】:

    标签: android encoding utf-8 webview xamarin.android


    【解决方案1】:

    你用过javascript吗

    atob()

    在将其发布到 webview 之前解码已使用 base-64 编码编码的数据字符串的方法。

    【讨论】:

    • 我没有。 WebView 只需要显示 HTML,所以我不想添加在其中运行 javascript 的能力。我尝试使用 Convert.ToBase64String,将该字符串转换回字节数组,然后再次将其解码为 UTF8,但得到相同的结果。