在WebView中加载本地HTML文件?

44

我想将本地的HTML加载到WebView中,但不想使用"file:///",因为这样不允许使用cookies。有没有一种类似于"localhost"的方式可供使用?

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

5个回答

102

你只能这样做。这个解决方案从一个字符串变量加载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)


1
@user113215:但是你已经阅读了loadDataWithBaseURL()方法的文档链接,对吧?第四个参数被称为“编码”,所以我将变量命名为“encoding”……正如你在这里看到的一样,它用于输出字符集:http://myexperiencewithandroid.blogspot.de/2011/09/android-loaddatawithbaseurl.html……你还应该阅读这篇文章:http://en.wikipedia.org/wiki/Character_encoding……所以我不明白你的问题。 - user432219
在此示例中未使用任何(数据)方案,仅加载本地 HTML 数据应显示在 WebView 中:“如果基本 URL 使用数据方案,则此方法等效于调用 loadData() 并忽略 historyUrl。” - user432219
1
啊,这很令人困惑。我调查了Android源代码,你是正确的。使用data: URL方案调用此方法会导致调用nativeLoadUrl()(其中encoding表示Base64或URL编码),但否则此方法会导致调用nativeLoadData()(其中encoding表示字符集)。 - quietmint

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

        WebView view = (WebView) findViewById(R.id.webView1);
        try {
        InputStream input = getResources().openRawResource(R.raw.lights);
        Reader is = new BufferedReader(
                new InputStreamReader(input, "windows-1252"));


            //InputStream input = getAssets().open("ws.TXT");
            int size;
            size = input.available();
            byte[] buffer = new byte[size];
            input.read(buffer);
            input.close();
            // byte buffer into a string
            javascrips = new String(buffer);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // String html = readFile(is);

        view.loadDataWithBaseURL("file:///android_res/raw/", javascrips, "text/html",
                "UTF-8", null);
    }

不错的代码片段,只是缺少定义 String javascrips; - SoftWyer
JavaScript 是任何 HTML 文件中的文本。例如:<html><body> ... </html> - Viren Savaliya
我正在采取额外的步骤。webview.loadUrl("file:///android_res/raw/help.html"); - wjl
view.loadDataWithBaseURL("file:///android_res/raw/" 提供本地图片路径,其中网页是由文本(JavaScript字符串)创建的,并且需要图像在其中定位的路径。 - Viren Savaliya

9

试试这段代码。它对我有效。

WebView mDesc = findViewById(R.id.descWv);
WebSettings settings = mDesc.getSettings();
settings.setDefaultTextEncodingName("utf-8");
mDesc.loadData(mDescText, "text/html; charset=utf-8",null);

mDesc是什么?如果没有它,这个答案就毫无意义。 - Ky -
1
@Supuhstar 感谢您的关注。"mDesc" 是一个 webview。我已经编辑了我的代码。 - Alican Temel

4

如果你想通过安卓访问本地主机localhost,你需要使用http://10.0.2.2:35643/,其中的35643是具体端口,如有需要。


0

以下代码对我有效。

String base64EncodedString = null;
try {
    base64EncodedString = android.util.Base64.encodeToString(
        (preString+mailContent.getBody() + postString).getBytes("UTF-8"), 
        android.util.Base64.DEFAULT);
} catch (UnsupportedEncodingException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
if (base64EncodedString != null)
{
    wvMailContent.loadData(base64EncodedString, "text/html; charset=utf-8", "base64");  
}
else
{
wvMailContent.loadData(preString+mailContent.getBody() + postString, "text/html; charset=utf-8", "utf-8");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接