机器人的WebView的JavaScript资产 [英] Android WebView Javascript from assets

查看:157
本文介绍了机器人的WebView的JavaScript资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天大家。希望下面可能是帮助他人,我不得不花了几乎整个晚上得到的东西的工作。简单的解决方案,简单的任务。 所以,问题是:我怎样才能让我的遥控器的HTML页面的JavaScript和图像从资产的文件夹(或者只是任何本地资源)加载

Good day everyone. Hope following might be helpful to others as I had to spend almost the whole evening to get the thing working. Simple solution for simple task. So, the question would be: "How can I make javascript and images on my remote html page be loaded from assets folder (or just any local resource)?"

推荐答案

答:
1.您必须加载HTML到字符串:

Answer:
1. You MUST load the HTML into string:

private String readHtml(String remoteUrl) {
    String out = "";
    BufferedReader in = null;
    try {
        URL url = new URL(remoteUrl);
        in = new BufferedReader(new InputStreamReader(url.openStream()));
        String str;
        while ((str = in.readLine()) != null) {
            out += str;
        }
    } catch (MalformedURLException e) { 
    } catch (IOException e) { 
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return out;
}


2.加载的WebView与基地网址:


2. Load WebView with base URL:

String html = readHtml("http://mydomain.com/my.html");
mWebView.loadDataWithBaseURL("file:///android_asset/", html, "text/html", "utf-8", "");

在这种特殊情况下,你应该有你想要使用的页面上的某个地方在项目的资产文件夹所在的所有的.js文件。例如:

In this particular case you should have all .js files you want to use on the page to reside somewhere under "assets" folder of project. For example:

/MyProject/assets/jquery.min.js


3.在您的远程HTML页面,您必须加载驻留在您的应用程序的.js和.css文件,如:


3. In your remote html page you have to load .js and .css files that reside in your application like:

<script src="file:///android_asset/jquery.min.js" type="text/javascript"></script>

同样适用于像图像的所有其他地方的资源,等等。他们的道路已经开始

the same applies to all other local resources like images, etc. their path has to start with

file:///android_asset/

一个的WebView会先加载您所提供的字符串的原始HTML,然后挑选的.js,.css和其他地方资源的开发,然后将加载远程的内容。

A WebView would first load the raw HTML you have provided as string, then pick .js, .css and other local resourses and then would load remote content.

这篇关于机器人的WebView的JavaScript资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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