加载本地HTML中的WebView?加载、HTML、WebView

2023-09-12 02:05:51 作者:让人心疼的孩子。

我要加载本地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()。因为饼干而使用的file:///是不允许的。

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