2016-02-12 57 views
1

的WebView不顯示任何加載XML到webView的

String dataStr = new String(data, "UTF-8"); 
Log.d("EPAB", dataStr); 
String mime = "text/html"; 
String encoding = "utf-8"; 
webView.getSettings().setJavaScriptEnabled(true); 
webView.loadDataWithBaseURL(null, dataStr, mime, encoding, null); 

尊者是dataStr

<?xml version="1.0" encoding="UTF-8"?> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title/> 
    <link rel="stylesheet" href="style.css" type="text/css"/> 
    <link rel="stylesheet" href="style.css" type="text/css"/> 
</head> 
<body class="z"> 
    <span id="id1"><div class="title1"> 
    <p class="p">ЧАСТЬ ПЕРВАЯ</p> 
    </div></span> 
</body> 
</html> 
+0

嘗試將Web頁面加載到webview? – drWisdom

+0

這個html在哪裏?在資產文件夾中,在任何字符串變量或任何網站上? – Rohit5k2

+0

它是從字節數組中進行字符串編碼的。資產文件夾中的file.epub的字節數組 –

回答

1

嘗試添加webView.setWebChromeClient(new WebChromeClient());

如果不行dataStr文本複製到「data.html 「文件並將其保存在」Assest「中,然後加載如下的HTML頁面

webView.setWebChromeClient(new WebChromeClient()); webView.loadUrl("file:///android_asset/www/data.html");

- 編輯 -

要發送文本到HTML文件

String text = "" ; //text read from file 
WebView webView = (WebView) findViewById(R.id.webView1); 
      WebSettings webSettings = webView.getSettings(); 
      webSettings.setJavaScriptEnabled(true); 
      webView.setWebChromeClient(new WebChromeClient()); 
      webView.loadUrl("file:///android_asset/www/data.html"); 
      webView .addJavascriptInterface(new webViewInterface(), "Data"); // Data will be used from html 

在webViewInterface類

public class webViewInterface { 
    private String text; 

    public webViewInterface(String fileText) { 
     this.text = fileText; 
    } 

    @JavascriptInterface 
    public String getText() { 
     return text; 
    } 
} 

在data.html文件

<?xml version="1.0" encoding="UTF-8"?> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title/> 
    <link rel="stylesheet" href="style.css" type="text/css"/> 
    <link rel="stylesheet" href="style.css" type="text/css"/> 
    <script type="text/javascript"> 
</head> 
<body class="z"> 
    var data = Data.getText() 
    <span id="id1"><div class="title1"> 
    <p class="p">data</p> 
    </div></span> 
</body> 
</html> 
+0

沒有CSS,它的工作完美。你的方式不適合我 –

+0

問題是,webview不能加載css,如果你將使用css文件,你需要將它移動到Assest並使用此代碼webView.setWebChromeClient(new WebChromeClient()); webView.loadUrl(「file:///android_asset/www/data.html」); – Sally

+0

但我從文件中讀取這些數據。也許你知道另一種顯示epub書的方式嗎? –