131

I would like to create a simple HTTP server in Android for serving some content to a client.

Any advice on how to build the server or use any existing library?

Jonathan Soifer
  • 2,715
  • 6
  • 27
  • 50
biquillo
  • 6,469
  • 7
  • 37
  • 40
  • if all you want is a web server , you can check out this tutorial: http://www.devlper.com/2010/12/a-bare-minimum-web-server-for-android-platform/ and its sample : https://code.google.com/p/krvarma-android-samples/source/browse/#svn%2Ftrunk%2Faws . they are both very old, but after some tweaks it will still work fine. – android developer Jun 16 '14 at 21:25

6 Answers6

142

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

Alexander Mironov
  • 3,095
  • 26
  • 28
Konstantin Milyutin
  • 11,946
  • 11
  • 59
  • 85
26

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.

Paul Hawke
  • 1,141
  • 10
  • 12
  • 2
    Where can I find a tutorial/sample of how to use it? for example to serve a file located on the device , so that others can download it (or stream from it) ? – android developer Jun 16 '14 at 20:37
10

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.

Community
  • 1
  • 1
Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
7

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

Alexsander Akers
  • 15,967
  • 12
  • 58
  • 83
Dmitriy R
  • 613
  • 1
  • 5
  • 12
  • 1
    This is fat by Android standards (100kb). Good if you want a serious server in java rather than just simple file serving or a remote config/debug/status interface into your app. – ahcox Apr 14 '12 at 15:15
5

If you are using kotlin,consider these library. It's build for kotlin language.

AndroidHttpServer is a simple demo using ServerSocket to handle http request

https://github.com/weeChanc/AndroidHttpServer

https://github.com/ktorio/ktor

AndroidHttpServer is very small , but the feature is less as well.

Ktor is a very nice library,and the usage is simple too

steven chan
  • 97
  • 2
  • 10
3

You can try Restlet edition for android:

The source can be downloaded from Restlet website:

Jav_Rock
  • 22,059
  • 20
  • 123
  • 164
Bharath
  • 39
  • 1