如何在webview中加载html字符串?

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

我有一个包含以下内容的html字符串:

    <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
    <html>
      <head>
      <meta http-equiv="content-type" content="text/html; charset=windows-1250">
      <meta name="spanish press" content="spain, spanish newspaper, news,economy,politics,sports">  
      <title></title>
      </head>
      <body id="body">  
<!-- The following code will render a clickable image ad in the page -->
        <script src="http://www.myscript.com/a"></script>
      </body>
    </html>

我需要将该网站显示到 Android 中的网络视图中。

我尝试过这一切:

webView.loadDataWithBaseURL(null, txt, "text/html", "UTF-8", null);
webView.loadDataWithBaseURL("x-data://base", txt, "text/html", "UTF-8", null);      
webView.loadDataWithBaseURL("notreal/", txt, "text/htm", "utf-8",null);

我还尝试删除 DOCTYPE 标签:

txt=txt.replace("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">", "");

这些人都没有工作。我刚刚实现了将字符串显示到 webview(html 代码)中,但不是必须使用该 html 代码创建的网站。

出了什么问题?

android webview android-webview
5个回答
161
投票

在 WebView 中加载数据。调用WebView的loadData()方法

wv.loadData(yourData, "text/html", "UTF-8");

你可以查看这个例子

http://developer.android.com/reference/android/webkit/WebView.html

[编辑1]

您应该在 -- " 之前添加 -- \ -- 例如 --> name=\"spanish press\"

下面的字符串对我有用

String webData =  "<!DOCTYPE html><head> <meta http-equiv=\"Content-Type\" " +
"content=\"text/html; charset=utf-8\"> <html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=windows-1250\">"+
 "<meta name=\"spanish press\" content=\"spain, spanish newspaper, news,economy,politics,sports\"><title></title></head><body id=\"body\">"+
"<script src=\"http://www.myscript.com/a\"></script>şlkasşldkasşdksaşdkaşskdşk</body></html>";

18
投票

你也可以试试这个

   final WebView webView = new WebView(this);
            webView.loadDataWithBaseURL(null, content, "text/html", "UTF-8", null);

12
投票

从资产 html 文件中读取

ViewGroup webGroup;
  String content = readContent("content/ganji.html");

        final WebView webView = new WebView(this);
        webView.loadDataWithBaseURL(null, content, "text/html", "UTF-8", null);

        webGroup.addView(webView);

1
投票

我有同样的要求,我是按照以下方式完成的。你也可以试试这个。

使用loadData方法

web.loadData("""
    <p style='text-align:center'>
        <img class='aligncenter size-full wp-image-1607' title='' src="+movImage+" alt='' width='240px' height='180px' />
    </p>
    <p>
        <center>
            <U>
                <H2>"+movName+"("+movYear+")</H2>
            </U>
        </center>
    </p>
    <p><strong>Director : </strong>"+movDirector+"</p>
    <p><strong>Producer : </strong>"+movProducer+"</p>
    <p><strong>Character : </strong>"+movActedAs+"</p>
    <p><strong>Summary : </strong>"+movAnecdotes+"</p>
    <p><strong>Synopsis : </strong>"+movSynopsis+"</p>
    """,
   "text/html",
   "UTF-8"
);

movDirector movProducer 都是我的字符串变量。

简而言之,我保留了 URL 的自定义样式。


0
投票

如果您正在 JetpackCompose 中寻找某些内容,这可以帮助您:

@Composable 
fun HtmlTextVisualizerComponent(textFromData: String) {
val mimeType = "text/html"
val encoding = "UTF-8"

LazyColumn(
    modifier = Modifier
        .padding(
            start = 24.dp,
            end = 24.dp,
            top = 16.dp,
            bottom = 24.dp,
        ),
) {
    items(1) {
        val state = rememberWebViewState(
            url = "data:$mimeType;$encoding,$textFromData",
        )
        val navigator = rememberWebViewNavigator()
        WebView(
            state = state,
            modifier = Modifier.fillMaxSize(),
            navigator = navigator,
            onCreated = {
                it.settings.javaScriptEnabled = true
            },
        )
    }
  }
}

一定不要忘记在你的 gradle 中添加依赖项:

 implementation "com.google.accompanist:accompanist-webview:0.30.1"
© www.soinside.com 2019 - 2024. All rights reserved.