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

时间:2013-11-12 12:05:57

标签: android twitter webview

我的WebView包含一些HTML数据存在问题(我不会加载网址,而是加载HTML中的自定义内容)。我用:

mWebView.loadData(mContent, "text/html; charset=utf-8", "utf-8");

在这个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>

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

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

有谁知道我该怎么做?

3 个答案:

答案 0 :(得分:5)

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

您可以将来自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>

如果您无法控制传入的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();

答案 1 :(得分:0)

调用loadDataWithBaseURL而不是loadData,但是不必使用https://twitter.com。只需使用您的网站协议和域,就像这样:

loadDataWithBaseURL(“ https://mywebsite.com”,mContent,“ text / html”,“ UTF-8”,null);

答案 2 :(得分:0)

请查看此博客以获取更多详细信息https://medium.com/@sajaljain98/embedded-tweets-in-android-webview-1363012746e3

private fun handleInWebiew() {
val value : String = "<blockquote class="twitter-tweet"><p lang="hi" dir="ltr">T 3644 -<br>छू लो तो चरण<br>अड़ा दो तो टांग<br>धंस जाए तो पैर<br>फिसल जाए तो पाँव<br>आगे बढ़ना हो तो कदम<br>राह में चिह्न छोड़े तो पद<br>प्रभु के हों तो पदुका *<br>गधे की पड़े तो *दुलत्ती<br>घुंघरु बांधो तो पग<br>खाने के लिए टंगड़ी<br>खेलने के लिए लंगड़ी<br><br>अंग्रेज़ी में only , LEG</p>&mdash; Amitabh Bachchan (@SrBachchan) <a href="https://twitter.com/SrBachchan/status/1307062341390020609?ref_src=twsrc%5Etfw">September 18, 2020</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>"
val webView = findViewById<>(R.id.webView)
webView.visibility = View.INVISIBLE //so that we can show progress bar util every thing is loaded
val progressBar = findViewById<>(R.id.progress_loader)
if (!TextUtils.isEmpty(value)) {
    Log.d(TAG, "bindData: ")
    val webSettings = webView.settings
    webView.setWebChromeClient(WebChromeClient())
    webSettings.javaScriptEnabled = true
    webSettings.domStorageEnabled = true
    webSettings.loadsImagesAutomatically = true
    webSettings.defaultTextEncodingName = "UTF-8"
    webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL)
    webSettings.setUseWideViewPort(false)
    webView.setHorizontalScrollBarEnabled(false);
    webView.setVerticalScrollBarEnabled(false);
    webView.setScrollContainer(false);
    webView.setOnTouchListener { v: View?, event: MotionEvent -> event.action == MotionEvent.ACTION_MOVE }
    webView.webViewClient = object : WebViewClient() {
        override fun onPageFinished(view: WebView?, url: String?) {
            Log.d(TAG, "onPageFinished: url $url")
//There is some delay in loading block-quote so avoid showing html without //loading js putting this fake delay
            Handler().postDelayed({
                if (progressBar != null) {
                    progressBar.visibility = View.GONE
                }
                webView.visibility = View.VISIBLE
            }, 3000)
        }

        override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
            super.onPageStarted(view, url, favicon)
        }


        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
            Log.d(TAG, "shouldOverrideUrlLoading: url " + url)
            if (!TextUtils.isEmpty(url)) {
                val uri = Uri.parse(url)
                val twitter = Intent(Intent.ACTION_VIEW, uri)
                twitter.setPackage("com.twitter.android")
                try {
                    mContext.startActivity(twitter)
                } catch (e: ActivityNotFoundException) {
                    mContext.startActivity(Intent(Intent.ACTION_VIEW,
                            Uri.parse(url)))
                }
            }
            return true
        }
    }
    try {
        webView.loadDataWithBaseURL("https://twitter.com", value, "text/html", "utf-8", null)
    } catch (e: Exception) {
        e.printStackTrace()
    }
} else {
    if (progressBar != null)
        progressBar.visibility = View.GONE
}}