android - 在 WebView 中加载本地 html?

标签 android webview

我想在不使用“file:///”的情况下将本地 html 加载到 WebView 中,因为这不允许使用 cookie。有没有办法使用类似“localhost”的东西?

其次,我找不到在 getSettings() 中启用 cookie 的方法。因为使用“file:///”时不允许使用 cookie。

最佳答案

你只能做这样的事情。此解决方案从字符串变量加载 HTML:

String html = "<html><body>Hello, World!</body></html>";
String mime = "text/html";
String encoding = "utf-8";

WebView myWebView = (WebView)this.findViewById(R.id.myWebView);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadDataWithBaseURL(null, html, mime, encoding, null);

编辑:尝试设置 loadDataWithBaseURL() 的第一个参数(baseURL)满足您的需求

关于android - 在 WebView 中加载本地 html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4543349/

相关文章:

java - JSONArray 未正确返回

Android - 我似乎没有 ANIM 文件夹或 MAINANIM.xml 文档...这是为什么?

android - 如何在 Parse 设置中获取 applicationID 和服务器?

android - 在 webview android 中截取视频的屏幕截图

java - 抽屉导航中 webview 的 findViewById 问题

android - 绘制带有空白区域的位图

java - 停止记住表单数据

javascript - 从 android webView 打开相机

android - webview 使用自定义字体后

android - 如何在不丢失其先前状态的情况下调用任何应用程序?