【问题标题】:How to pass html string to webview on android?如何将html字符串传递给android上的webview
【发布时间】:2012-02-17 17:45:04
【问题描述】:

您好,我正在解析 xml,然后将其加载到 Web 视图,解析后我创建了四个字符串,以便我可以将所有字符串附加到一个视图。我能够在网络视图上获得两个视图,但不能获得前两个字符串。

请用我的代码建议我,我哪里出错了,以及在 Web 视图上获取格式化的 html 字符串的正确方法是什么。请查看我的代码并帮助我解决此问题。

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        String chapterTitle = "";
        String SubChapterTitle="";
        String chapterIntro ="";
        String chapterContent="";
        View view = convertView;
        if (convertView == null) {
            // view = inflater.inflate(resourceid, null);
            view = getLayoutInflater().inflate(R.layout.webviewitem, null);
        }
        synchronized (view) {
            WebView wv = (WebView) view.findViewById(R.id.contentWebView);

            WebSettings settings = wv.getSettings();
            settings.setUseWideViewPort(true);
            settings.setLoadWithOverviewMode(true);
            settings.setJavaScriptEnabled(true);
            settings.setDefaultZoom(ZoomDensity.FAR);
            // wv.setBackgroundColor(0);
            wv.setVerticalScrollBarEnabled(false);
            wv.setHorizontalScrollBarEnabled(false);
            /*String txtChapTitle = Intro.book.getsecretList().get(position)
                    .getChtitle().toString();*/

            if (!(Intro.book.getsecretList().get(position).getChtitle()
                    .toString().equals(""))){
            chapterTitle = "<b><fontSize=4>"+Intro.book.getsecretList().get(position)
            .getChtitle().toString()+"</font></b>";
            }
            if (!(Intro.book.getsecretList().get(position)
                    .getSubtitle() == null)) {
                SubChapterTitle = "<b><fontSize=4>"+Intro.book.getsecretList().get(position)
                .getSubtitle().toString()+"</font></b>";
            }
            if (!(Intro.book.getsecretList().get(position)
                    .getIntro() == null)) {
            chapterIntro = "<b><fontSize=2>"+Intro.book.getsecretList().get(position)
                .getIntro().toString()+"</font></b>";
            }
            if (!(Intro.book.getsecretList().get(position)
                    .getContent() == null)) {
            chapterContent = "<fontSize=2>"+Intro.book.getsecretList().get(position)
                .getContent().toString()+"</font>";
            }

            StringBuilder content = new StringBuilder();
            content.append(chapterTitle+SubChapterTitle+chapterIntro+chapterContent);

            JsInterface Jsi = new JsInterface();
            Jsi.wordDef = content ;
            Log.v("Content", "" +content);
            wv.addJavascriptInterface(Jsi, "interfaces");

            wv.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageFinished(WebView view, String url) {
                    view.setHapticFeedbackEnabled(false);
                }
            });

            wv.setWebChromeClient(new WebChromeClient() {
                @Override
                public boolean onJsAlert(WebView view, String url,
                        String message, JsResult result) {
                    return super.onJsAlert(view, url, message, result);
                }
            });

            wv.loadUrl("file:///android_asset/wordview.html");
        }
        return view;
    }
}

我能够在网络视图上获得 chapterIntro 和 chaptercontent,但不是前两个字符串,请朋友们帮助我。

【问题讨论】:

    标签: android webview


    【解决方案1】:

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

    webView.loadData(yourData, "text/html; charset=utf-8", "UTF-8");
    

    你可以看看这个例子

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

    【讨论】:

    • 我试过了,仍然没有发生也试过 loadbaserul
    • 这应该适用于包含 javascript 的字符串吗?它不适合我
    • 应该是webView.loadData(yourData, "text/html; charset=utf-8", "UTF-8");
    • "text/html; charset=utf-8" 有很大的不同,没有它,符号字符将无法正确呈现。
    • String htmlData = "你的 HTML 字符串"; mWebView.loadData(htmlData, "text/html", "UTF-8");
    【解决方案2】:

    我已经通过下面的行成功完成了

     //data == html data which you want to load
     String data = "Your data which you want to load";
    
     WebView webview = (WebView)this.findViewById(R.id.webview);
     webview.getSettings().setJavaScriptEnabled(true);
     webview.loadData(data, "text/html; charset=utf-8", "UTF-8");
    

    或者你可以试试

     webview.loadDataWithBaseURL(null, data, "text/html", "utf-8", null);
    

    【讨论】:

    • 你把这个加到 index.php/index.html 的 中了吗?
    • 如果你愿意,否则没关系
    • 它会在稍有延迟后更新 UI。如何解决这个问题?
    • 如果我想使用 webView.loadUrl("javascript:MyFunction( data, "text/html" 将 html 传递给 javascript 函数,我可以使用 "text/html", "UTF-8" , "UTF-8")?
    • 不,我没有这样用过
    【解决方案3】:

    传递 null 会更好。完整代码如下:

    WebView wv = (WebView)this.findViewById(R.id.myWebView);
    wv.getSettings().setJavaScriptEnabled(true);
    wv.loadDataWithBaseURL(null, "<html>...</html>", "text/html", "utf-8", null);
    

    【讨论】:

    • 你把这个加到 index.php/index.html 的 中了吗?
    • @mthelelibeseti 这是 Android 代码,而不是 HTML。还是我误解了你的问题?
    • 它是所有类型的 html 字符串的有效解决方案。谢谢@JeffreyNeo
    • 真正正确的答案!以上答案对我不起作用,但这个答案对我有用。谢谢@JeffreyNeo
    【解决方案4】:

    我正在使用一些带有一些事件的按钮,转换后的图像文件来自服务器。 加载普通数据对我不起作用,转换为 Base64 工作正常。

    String unencodedHtml ="<html><body>'%28' is the code for '('</body></html>";
    tring encodedHtml = Base64.encodeToString(unencodedHtml.getBytes(), Base64.NO_PADDING);
    webView.loadData(encodedHtml, "text/html", "base64");
    

    WebView上查找详细信息

    【讨论】:

    • +1 用于编码 HTML。您还可以将正确的字符集传递给getBytes(),如下所示:unencodedHtml.getBytes("UTF-8"),并在 MIME 类型中定义字符集,如下所示:"text/html; charset=utf-8"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 2013-02-15
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    相关资源
    最近更新 更多