https://github.com/lopspower/androidwebserver
Android Web Server (NanoHttpd)
https://github.com/lopspower/androidwebserver
Last synced: 6 months ago
JSON representation
Android Web Server (NanoHttpd)
- Host: GitHub
- URL: https://github.com/lopspower/androidwebserver
- Owner: lopspower
- Created: 2013-07-08T16:57:57.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2020-02-05T10:26:11.000Z (over 5 years ago)
- Last Synced: 2025-03-30T07:11:10.592Z (6 months ago)
- Language: Java
- Size: 449 KB
- Stars: 439
- Watchers: 29
- Forks: 110
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
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";
msg += "Your name:
\n";
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).