开发者

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 adva开发者_如何学编程nced!


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");


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜