0

I have an Android Activity with WebView in it.
I have all my HTML, CSS and JavaScript files in assets folder.

I can run the index.html from assets in my WebView but it doesn't work correctly.
What I need is a VERY simple HTTP server running in that directory.

How can I do that?

Basically as simple as this:

SimpleHTTPServer running on a mac.

$ cd /home/somedir
$ python -m SimpleHTTPServer

If it can't be assets directory, I can use any directory as long as I can set it.

any ideas?

thank you!

=================================

EDIT

what I mean by "it doesn't work correctly" is that the index.html and other assets files make AJAX calls and to view the HTML correctly with the calls, there needs to be a local HTTP server running on that directory.

ᴛʜᴇᴘᴀᴛᴇʟ
  • 4,466
  • 5
  • 39
  • 73

2 Answers2

1

You can do something like:

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setJavaScriptEnabled(true);
myWebView.loadUrl("file:///android_asset/index.html");

You dont need to be running a web server on your android app.

A more detailed example of this can be found here: https://developer.chrome.com/multidevice/webview/gettingstarted#loading_html_files_from_the_file_system

Marty
  • 2,965
  • 4
  • 30
  • 45
  • I'm already doing that but that will not load angularJS or ajax. All my content of the html page isn't there, that is why I said "it doesn't work correctly" before. – ᴛʜᴇᴘᴀᴛᴇʟ Oct 09 '15 at 16:21
  • Have you tried to call `setJavaScriptEnabled` on the Web View? I have updated the answer – Marty Oct 09 '15 at 16:23
  • I tried that.. still doesn't work. It doesn't even work on a computer chrome browser if there is no local server there. I had to do `$ python -m SimpleHTTPServer` on mac terminal to make it work on the computer. – ᴛʜᴇᴘᴀᴛᴇʟ Oct 09 '15 at 16:28
  • this does not handle dynamic content like AJAX – pskink Oct 09 '15 at 16:59
0

thanks to @Marty's answer, I was able to take it a step further and make it work.

WebView myWebView = (WebView)findViewById(R.id.mainWebView);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
myWebView.loadUrl("file:///android_asset/index.html");

setAllowUniversalAccessFromFileURLs made it all work!

hope this helps someone. thanks!

ᴛʜᴇᴘᴀᴛᴇʟ
  • 4,466
  • 5
  • 39
  • 73