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

6

133

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?

Osteen answered 13/6, 2011 at 10:52 Comment(0)
S
145

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

Saddlecloth answered 13/6, 2011 at 10:57 Comment(11)
Amazing. Probably saved a month of work. Can you use it in a commercial product?Pich
You should read about Modified BSD licence. If it allows using code in commercial product or not.Saddlecloth
I can't seem to make images display in my html page. What directory should be used in the html for images to show? Thanks.Alexandraalexandre
path to images is relative regarging the html page. it's not a problem of http server, it's bad html..Saddlecloth
Can I serve a file which is currently being added to the homeDir . For example I am copying a file named f1 in homeDir (using javacode). Now the file copying is in progress and if some one send request for the file f1 Can server will process the request?Bidget
Please look at the documentation. I used it quite long ago.Saddlecloth
Hey Guys I am using the NanoHTTPD server in my android app but it gives me java.net.BindException : Permission Denied Is there anything special I have to do for it?Bidget
As far as I remember, you have to request permissions for your app in the special file. did you do it?Saddlecloth
@damluar - did you use it in Android?Convincing
yes, but it was around 3 years agoSaddlecloth
SSL support? The git page claims "see the 'ssl-support' branch" but when I look at the branches I see and that is not one of them...Mcauley
W
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.

Weakness answered 21/5, 2013 at 4:37 Comment(1)
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) ?Cibis
E
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.

Eventful answered 9/5, 2016 at 18:12 Comment(0)
B
7

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

Bananas answered 29/6, 2011 at 7:7 Comment(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.Manyplies
B
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

Backstitch answered 29/3, 2018 at 11:30 Comment(0)
W
3

You can try Restlet edition for android:

The source can be downloaded from Restlet website:

Whenas answered 12/9, 2012 at 22:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.