的WebView加载它包含的.js为&lt本地HTML文件,脚本SRC> [英] webview to load the local html file which contains .js as <script src>

查看:190
本文介绍了的WebView加载它包含的.js为&lt本地HTML文件,脚本SRC>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把的的.html 的(其中包括的的的.js 的网络链接)在入资产的文件,现在我试图加载网址

I place the .html (which consist of the .js web link) file in the assest, now I am trying to load url

mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setPluginsEnabled(true);
        mWebView.getSettings().setAllowFileAccess(true);

        mWebView.loadUrl("file:///android_asset/anim1.html");

我在这里无法播放的的.js 的文件,等待积极响应。

Here I am unable to play the .js file, waiting for the positive response.

推荐答案

首先,我认为这是不可能的,你的真正的想从资产访问HTML和JavaScript文件,作为换入资产不能(也不应该)一旦应用程序已经打包并签署更新。这意味着,你的HTML和JavaScript的资产,则不能对其进行更新。解决这个问题的方法是保存在内部或外部存储的HTML和JavaScript,并从那里使用它。

First of all, I think it's unlikely that you really want to be accessing HTML and JavaScript files from the assets, as the assets cannot (and shouldn't) be updated once the app has been packaged and signed. This means, that with your HTML and JavaScript in the assets, you cannot update them. The approach to solve that problem is to save the HTML and JavaScript in the internal or the external storage, and use it from there.

其次,如果你使用的方法,我刚才解释,你再也不能使用 WebView.loadUrl()的方法,而是你需要使用 WebView.loadDataWithBaseUrl()方法。像这样的:

Secondly, if you use that approach I just explained, you cannot any longer use the WebView.loadUrl() method, but instead you will need to use the WebView.loadDataWithBaseUrl() method. Like this:

webView.loadDataWithBaseURL("http://some_url/", "some_data", "text/html", "utf-8", null);

那么,some_data将尝试加载HTML页面,如<一看到href="http://stackoverflow.com/questions/5350987/load-web-application-into-a-webview-using-local-paths-for-images-stored-in-the-a">this previous SO回答。当它试图加载一个JavaScript资源,如 /scripts/lib.js ,它会被解析为的http:// some_url /脚本/ lib目录.js文件,和web视图将尝试加载资源。当然不能,这就是为什么你(在Java中)将需要扩展 WebViewClient 类,并为 shouldInterceptRequest(web视图的实现西弗吉尼亚州,字符串URL)方法。

Then, "some_data" will be tried loaded as an HTML page, as seen in this previous SO answer. When it tries to load a JavaScript resource, like /scripts/lib.js, it will be resolved to http://some_url/scripts/lib.js, and the WebView will try to load that resource. Of course it cannot, and that is why you (in Java) will need to extend the WebViewClient class, and provide an implementation for the shouldInterceptRequest(WebView wv, String url) method.

在任何资源尝试在Web页面中获取,'shouldInterceptRequest'将被调用,它的返回值作为资源。

When any resource is tried to be fetched in the web page, ´shouldInterceptRequest´ will be called, and its return value used as the resource.

这篇关于的WebView加载它包含的.js为&lt本地HTML文件,脚本SRC&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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