WebView不显示html文件中的数据

发布于 2024-11-08 02:12:29 字数 643 浏览 1 评论 0原文

我的 Android 应用程序上有一个页面,它将使用下面的代码显示 html 文件的内容 -

 public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.intro);

      WebView wv = (WebView) findViewById(R.id.WebView01);
      try {
          InputStream fin;
          fin = getAssets().open("Preface.html");
          byte[] buffer = new byte[fin.available()];
          fin.read(buffer);
          fin.close();
          wv.loadData(new String(buffer), "text/html", "UTF-8");
       } catch (IOException e) {
          e.printStackTrace();
       }
 }

代码运行正常,但内容不显示在 web 视图中,我做错了什么?

I have a page on my Android app which will display the contents of an html file using the code below -

 public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.intro);

      WebView wv = (WebView) findViewById(R.id.WebView01);
      try {
          InputStream fin;
          fin = getAssets().open("Preface.html");
          byte[] buffer = new byte[fin.available()];
          fin.read(buffer);
          fin.close();
          wv.loadData(new String(buffer), "text/html", "UTF-8");
       } catch (IOException e) {
          e.printStackTrace();
       }
 }

The code runs ok but the contents don't show in the webview, what am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

a√萤火虫的光℡ 2024-11-15 02:12:29

wv.loadUrl("file:///android_asset/Preface.html");

确保您的文件 Preface.html 位于您的 android assets/ 文件夹内

,或者如果您的 html 文件包含 javascript 代码,请启用 javascript 支持,

WebView wv = (WebView) findViewById(R.id.WebView01);
      try {
          InputStream fin;
          fin = getAssets().open("Preface.html");
          byte[] buffer = new byte[fin.available()];
          fin.read(buffer);
          fin.close();
          wv.loadData(new String(buffer), "text/html", "UTF-8");
    WebSettings webSettings = wv.getSettings();
    wv.setJavaScriptEnabled(true);
       } catch (IOException e) {
          e.printStackTrace();
       }

如果这不能解决您的问题,请粘贴您的 html 代码。

use

wv.loadUrl("file:///android_asset/Preface.html");

be sure your file Preface.html is inside your android assets/ folder

or if your html file contains javascript code enable javascript support with

WebView wv = (WebView) findViewById(R.id.WebView01);
      try {
          InputStream fin;
          fin = getAssets().open("Preface.html");
          byte[] buffer = new byte[fin.available()];
          fin.read(buffer);
          fin.close();
          wv.loadData(new String(buffer), "text/html", "UTF-8");
    WebSettings webSettings = wv.getSettings();
    wv.setJavaScriptEnabled(true);
       } catch (IOException e) {
          e.printStackTrace();
       }

if this don't solve your problem paste your html code.

多彩岁月 2024-11-15 02:12:29

首先在LogCat中显示你下载的文件内容,看看下载是否成功

First , display the content of the file you download in the LogCat, and see if the download is okey or not

一身仙ぐ女味 2024-11-15 02:12:29

有时会发生的情况是,您的屏幕尺寸变得太小,无法在屏幕上显示内容。确保您的屏幕使用更大的显示分辨率。

Sometimes what happens is,your screen size becomes too small for the content to display on the screen. see to it that you are using a larger display resolution for your screen.

凑诗 2024-11-15 02:12:29

我的以下代码从 Url 加载 html 内容并将其显示在 Web 视图中:

MainActivity.java

String htmlContent = getHtmlContent();
            if (htmlContent != null) {
                webView.getSettings().setBuiltInZoomControls(true);
                webView.loadData(htmlContent, fileType.endsWith("rfc822") ? "rfc822" : "text/html", "UTF-8");
            }

getHtmlContent 方法

DownloadTask task = new DownloadTask();

    try {
        String result = String.valueOf(task.execute(currentFileUrl).get());
        if (task.getStatus() == AsyncTask.Status.RUNNING) {
            loadingProgressBar.setVisibility(View.GONE);
        }
        return result;
    } catch (ExecutionException | InterruptedException e) {
        e.printStackTrace();
    }

    return null;

DownloadTask.java

public class DownloadTask extends AsyncTask<String, Void, StringBuilder> {


@Override
protected StringBuilder doInBackground(String... urls) {

    StringBuilder result = new StringBuilder();
    URL url;
    HttpURLConnection connection;

    try {
        url = new URL(urls[0]);
        connection = (HttpURLConnection) url.openConnection();
        InputStream in = connection.getInputStream();
        InputStreamReader reader = new InputStreamReader(in);
        int data = reader.read();

        while (data != -1) {
            char current = (char) data;
            result.append(current);
            data = reader.read();
        }

        return result;

    } catch (IOException e) {
        e.printStackTrace();
    }



    return null;
}

}

我遇到的一个问题是 WebView 没有显示内容,我通过设置 WebView webview = findViewById(R.id.webView); 解决了这个问题
并删除 webview = new WebView(this);

My following code loads html-content from a Url and displays it in the webview:

MainActivity.java

String htmlContent = getHtmlContent();
            if (htmlContent != null) {
                webView.getSettings().setBuiltInZoomControls(true);
                webView.loadData(htmlContent, fileType.endsWith("rfc822") ? "rfc822" : "text/html", "UTF-8");
            }

getHtmlContent Method

DownloadTask task = new DownloadTask();

    try {
        String result = String.valueOf(task.execute(currentFileUrl).get());
        if (task.getStatus() == AsyncTask.Status.RUNNING) {
            loadingProgressBar.setVisibility(View.GONE);
        }
        return result;
    } catch (ExecutionException | InterruptedException e) {
        e.printStackTrace();
    }

    return null;

and the DownloadTask.java class

public class DownloadTask extends AsyncTask<String, Void, StringBuilder> {


@Override
protected StringBuilder doInBackground(String... urls) {

    StringBuilder result = new StringBuilder();
    URL url;
    HttpURLConnection connection;

    try {
        url = new URL(urls[0]);
        connection = (HttpURLConnection) url.openConnection();
        InputStream in = connection.getInputStream();
        InputStreamReader reader = new InputStreamReader(in);
        int data = reader.read();

        while (data != -1) {
            char current = (char) data;
            result.append(current);
            data = reader.read();
        }

        return result;

    } catch (IOException e) {
        e.printStackTrace();
    }



    return null;
}

}

One problem that I had is the WebView wasn't desplaying the content, I solved it by setting WebView webview = findViewById(R.id.webView);
and removing webview = new WebView(this);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文