如何在Android WebView中正确嵌入推文?

PTo*_*uch 2 twitter android webview

我有一个包含一些HTML数据的WebView的问题(我不加载URL而是HTML中的自定义内容).我用:

mWebView.loadData(mContent, "text/html; charset=utf-8", "utf-8");
Run Code Online (Sandbox Code Playgroud)

在这个HTML代码(mContent)中,我有时会有一个Twitter blockquote嵌入代码,例如:

<blockquote class="twitter-tweet"><p>"Will Remote Play on the PS Vita be available for <a    style="color:#ff8600;" href="https://twitter.com/search?q=%23D3&src=hash">#D3</a> Ultimate Edition on <a style="color:#ff8600;" href="https://twitter.com/search?q=%23PS4&src=hash">#PS4</a>?" The answer is yes, and it's awesome! <a style="color:#ff8600;" href="http://t.co/Rg059nXZMF">pic.twitter.com/Rg059nXZMF</a></p> Diablo (@Diablo) <a style="color:#ff8600;" href="https://twitter.com/Diablo/statuses/400073137590530048">November 12, 2013</a></blockquote>
Run Code Online (Sandbox Code Playgroud)

所以有什么问题?好吧,我的WebView只显示没有样式或图像的推文文本.(此代码在我的iOS应用程序上完美运行.)

我在我的WebView上有setJavaScriptEnabled(true),用WebChromeClients和loadDataWithBaseURL尝试了几件事,但是我没有设法在我的WebView上很好地显示嵌入的推文.

有谁知道我该怎么做?

HBG*_*HBG 6

我有几种方法可以解决这个问题:

您可以将以下呼叫替换为loadData:

loadDataWithBaseURL("https://twitter.com", mContent, "text/html", "UTF-8", null);

或者,您可以确保html字符串(mContent)在其头部具有以下内容:

<script type="text/javascript" src="https://platform.twitter.com/widgets.js"></script>
Run Code Online (Sandbox Code Playgroud)

如果您无法控制传入的HTML,可以尝试使用Jsoup自行修改内容.

Document doc = Jsoup.parse(mContent);
doc.head().appendElement("script").attr("type", "text/javascript").attr("src", "https://platform.twitter.com/widgets.js");

// This is the html that includes the appropriate reference to Twitter's widgets.js script
String newHtml = doc.toString();
Run Code Online (Sandbox Code Playgroud)