Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a HTTP server in Android? [closed]

Tags:

android

http

People also ask

What is HTTP server in Android?

GitHub - piotrpolak/android-http-server: A complete zero-dependency implementation of a web server and a servlet container in Java with a sample Android application.

Can I run a server on my phone?

From the office of “We do this because we can” comes the ability to run a web server on an Android device. But this isn't just any old web server–this is one powered by Apache. The server in question is HTTP Server powered by Apache and it can be found on the Google Play Store.


Consider this one: https://github.com/NanoHttpd/nanohttpd. Very small, written in Java. I used it without any problem.


NanoHttpd works like a charm on Android -- we have code in production, in users hands, that's built on it.

The license absolutely allows commercial use of NanoHttpd, without any "viral" implications.


This can be done using ServerSocket, same as on JavaSE. This class is available on Android. android.permission.INTERNET is required.

The only more tricky part, you need a separate thread wait on the ServerSocket, servicing sub-sockets that come from its accept method. You also need to stop and resume this thread as needed. The simplest approach seems to kill the waiting thread by closing the ServerSocket. If you only need a server while your activity is on the top, starting and stopping ServerSocket thread can be rather elegantly tied to the activity life cycle methods. Also, if the server has multiple users, it may be good to service requests in the forked threads. If there is only one user, this may not be necessary.

If you need to tell the user on which IP is the server listening,use NetworkInterface.getNetworkInterfaces(), this question may tell extra tricks.

Finally, here there is possibly the complete minimal Android server that is very short, simple and may be easier to understand than finished end user applications, recommended in other answers.


Another server you can try http://tjws.sf.net, actually it already provides Android enabled version.