如何在android上将html字符串传递给webview

时间:2012-01-24 13:21:51

标签: android webview

您好我正在解析xml然后将其加载到Web视图,解析后我创建了四个字符串,以便我可以将所有字符串附加到一个视图。我能够在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,但不是前两个字符串请帮助我的朋友。

4 个答案:

答案 0 :(得分:167)

我已经通过以下线成功完成了

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

答案 1 :(得分:148)

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

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

您可以查看此示例

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

答案 2 :(得分:22)

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

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

答案 3 :(得分:0)

加载普通数据对我来说不起作用,转换为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的详细信息