在 WebView 中加载本地 html? [英] Load local html in WebView?

查看:35
本文介绍了在 WebView 中加载本地 html?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to load a local html into a WebView WITHOUT using "file:///" because that does not allow cookies. Is there a way to use something like "localhost" ?

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

Secondly, I could not find a way to enable cookies in the getSettings(). Because cookies are not allowed while using "file:///".

推荐答案

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

You can only do something like that. This solution load HTML from a String variable:

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() 满足你的需求

try to set the first parameter (the baseURL) of loadDataWithBaseURL() for your needs

这篇关于在 WebView 中加载本地 html?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆