16

我通过变量在响应中获取此 html 内容:String mresult=

<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Lorem Ipsum</title>
</head>
<body style="width:300px; color: #00000; ">

<p><strong> About us</strong> </p>
<p><strong> Lorem Ipsum</strong> is simply dummy text .</p>

<p><strong> Lorem Ipsum</strong> is simply dummy text </p>

<p><strong> Lorem Ipsum</strong> is simply dummy text </p>

</body></html>

它无法加载webview,我必须尝试像这样加载它:

web.loadDataWithBaseURL(null, mresult, "text/html", "UTF-8", null);

仍然webview显示空白白页。

xml文件中:

<WebView
        android:id="@+id/wV"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"          
        android:layout_margin="10dp"
        android:background="@android:color/transparent"
        android:cacheColorHint="#00000000" />

在活动中:

String mresult="Here my given data";
WebView web = (WebView)findViewById(R.id.wV);
web.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
});
web.loadData(mresult, "text/html; charset=UTF-8", null);
4

5 回答 5

24

以这种方式使用这个 Html_value:

String html_value = "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"><title>Lorem Ipsum</title></head><body style=\"width:300px; color: #00000; \"><p><strong> About us</strong> </p><p><strong> Lorem Ipsum</strong> is simply dummy text .</p><p><strong> Lorem Ipsum</strong> is simply dummy text </p><p><strong> Lorem Ipsum</strong> is simply dummy text </p></body></html>";

这用于 webview 然后只有它才会起作用,因为您的 html 标记没有正确关闭:

WebView browser = (WebView) findViewById(R.id.sample);
        browser.getSettings().setJavaScriptEnabled(true);
        browser.loadData(html_value, "text/html", "UTF-8");

输出:

在此处输入图像描述

于 2013-04-04T08:57:05.970 回答
4
    wb.loadDataWithBaseURL("", result, "text/html", "UTF-8", "");
于 2014-07-04T08:45:18.953 回答
2

试试这个 WebView 的代码:

WebView wv = (WebView) findViewById(R.id.webView);
wv.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
});
wv.loadData(mresult, "text/html; charset=UTF-8", null);
于 2013-04-04T07:56:58.060 回答
2

不知道你仍然没有加载,webview但给定的结果TextViewHtml如下格式显示

TextView  txtweb = (TextView) findViewById(R.id.txtweb);
txtweb.setText(Html.fromHtml(mresult));
于 2013-04-04T09:59:05.983 回答
1

用这个:

  webView.loadDataWithBaseURL("file:///android_asset/", htmlParsing, "text/html", "utf-8", null);
  webView.getSettings().setAllowFileAccess(true);

在 htmlparsing 中:只需传递 urs html 代码。

有用....

于 2014-07-23T04:24:52.787 回答