
AndroidWebServer
=========
[](https://android-arsenal.com/details/1/2847)
[](http://developer.android.com/index.html)
[](https://android-arsenal.com/api?level=8)
[](http://twitter.com/lopezmikhael)
This is a sample project for creating an **Android Web Server** using the [NanoHTTPD](https://github.com/NanoHttpd/nanohttpd) library.
Usage
-----
1. To make an **Android Web Server** add [NanoHTTPD](https://github.com/NanoHttpd/nanohttpd) dependency in your build.gradle file:
```groovy
compile 'org.nanohttpd:nanohttpd:2.2.0'
```
2. After that, you must create an **Android Web Server** Class this way:
```java
public class AndroidWebServer extends NanoHTTPD {
public AndroidWebServer(int port) {
super(port);
}
public AndroidWebServer(String hostname, int port) {
super(hostname, port);
}
//...
}
```
3. Add `serve()` method in your **Android Web Server** Class :
```java
@Override
public Response serve(IHTTPSession session) {
String msg = "Hello server
\n";
Map parms = session.getParms();
if (parms.get("username") == null) {
msg += "\n";
} else {
msg += "Hello, " + parms.get("username") + "!
";
}
return newFixedLengthResponse( msg + "\n" );
}
```
`serve()` is a very important method beacause this is the response sent by your web server.
4. You can now instantiate and start your server in your activity. (Full implementation [**here**](/app/src/main/java/com/mikhaellopez/androidwebserver/MainActivity.java))
```java
AndroidWebServer androidWebServer = new AndroidWebServer(port);
androidWebServer.start();
```
```java
androidWebServer.stop();
```
LICENCE
-----
**AndroidWebServer** by [Lopez Mikhael](http://mikhaellopez.com/) is licensed under a [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0).