【问题标题】:load a remote url with android webview and local css/js使用 android webview 和本地 css/js 加载远程 url
【发布时间】:2015-07-23 17:47:52
【问题描述】:

几天来...我试图找到一种解决方案来使用 android webview 加载远程 url。但是使用本地 css/js/images

但我做不到。

我认为这是不可能的,因为安全问题......对吧?

我想我可以使用一个技巧。

在远程服务器中,我只是在没有<head> 和<body> 的情况下发布我的网页:

//<html>
    // <head></head>
    // <body>
        ... body  code111 ...  // there is only this part in remote url page
    //</body>
//</html>

然后我在android_asset 文件夹中创建一个 index.html:

<html>
    <head>
        <link css ...
        <link java ...
    </head>
    <body>
        ... now put code111 here!
    </body>
</html>

所以现在我可以使用本地资源(在头部)

我是 android 和 java 的新手...我无法测试它...但是您认为这可能吗?

【问题讨论】:

标签: android webview


【解决方案1】:

好吧,让我们开始吧。

B 部分比较容易。

您可以在这里找到答案: Rendering HTML in a WebView with custom CSS

基本上,您要做的就是将 html 网站放在一个字符串变量上,例如:

   String website = "<html><head>" + "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />" + <"javascript call to file that i don't remember right now"/>" + "</head>"

然后将您的远程 HTML 代码附加到该网站。例如,您可以通过任何 Async HTTP 库来实现(LoopJ 的库非常好 loopj's http library)。

然后附加您网站的其余部分。

website += retrieved_website
website += "</body></html>"

最终使用 .loadDataWithBaseURL() 方法加载网站,如接受的响应中所述。

虽然我不得不说这是一种相当复杂的网站加载方式,但这是我的方法。

【讨论】:

  • 最好的方法是为您的网站和具有原生 UI 的原生应用创建一个网络服务。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多