Lin
Lin

Reputation: 2565

Run a webserver within an android application

I have an android application that uses a webview to display some of its screens. The problem - it takes a lot of time for the android webview pages to download the images from the remote server. Therefore I thought about starting a webserver within my android application (in a new thread) that will serve the static images.

I was able to start the webserver in a thread on port 8080 (from my application), but wasn't able to access it from my application via webview on url "10.0.2.2:8080", "127.0.0.1:8080" and "localhost:8080". Any ideas why I wasn't able to access my webserver?

Thanks in advanced!

Upvotes: 2

Views: 2747

Answers (3)

JamesG
JamesG

Reputation: 388

If you don't have a lot of images, or if the total size isn't too much you can just include your images as assets in your app.

You can load static HTML into a webview that references the images that you deployed with your app.

A quick reference for how to use local images in HTML for a webview is located here: http://www.androidsnippets.com/webview-with-custom-html-and-local-images

Upvotes: 1

Yekmer Simsek
Yekmer Simsek

Reputation: 4102

You do not need to run a server for loading static content. Android webview has a method loadDataWithBaseURL for this porpose

http://developer.android.com/reference/android/webkit/WebView.html#loadDataWithBaseURL%28java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang.String%29

Upvotes: 2

Brian Zhang
Brian Zhang

Reputation: 34

Would you consider directly load HTML data into webview instead?

String summary = "<html><body>You scored <b>192</b> points.</body></html>";
webview.loadData(summary, "text/html", "utf-8");

Upvotes: 2

Related Questions