AndroidWebServer
=========
[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-WebServer-lightgrey.svg?style=flat)](https://android-arsenal.com/details/1/2847)
[![Platform](https://img.shields.io/badge/platform-android-green.svg)](http://developer.android.com/index.html)
[![API](https://img.shields.io/badge/API-8%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=8)
[![Twitter](https://img.shields.io/badge/Twitter-@LopezMikhael-blue.svg?style=flat)](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).