ᴛʜᴇᴘᴀᴛᴇʟ
ᴛʜᴇᴘᴀᴛᴇʟ

Reputation: 4656

How to run HTTP server on android?

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.

Upvotes: 0

Views: 1796

Answers (2)

ᴛʜᴇᴘᴀᴛᴇʟ
ᴛʜᴇᴘᴀᴛᴇʟ

Reputation: 4656

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!

Upvotes: 0

Marty
Marty

Reputation: 2963

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

Upvotes: 1

Related Questions