No Description

Fruit of Eden 371da9736c Added Controllers 6 years ago
app 371da9736c Added Controllers 6 years ago
gradle b394d381de Initial 6 years ago
.gitignore 846096dd6b Added Controllers 6 years ago
AndroidWebServer-master.iml b394d381de Initial 6 years ago
README.md b394d381de Initial 6 years ago
build.gradle b394d381de Initial 6 years ago
gradlew b394d381de Initial 6 years ago
gradlew.bat b394d381de Initial 6 years ago
local.properties b394d381de Initial 6 years ago
settings.gradle b394d381de Initial 6 years ago

README.md

AndroidWebServer

Android Arsenal Platform API Twitter

This is a sample project for creating an Android Web Server using the NanoHTTPD library.

Usage

  1. To make an Android Web Server add NanoHTTPD dependency in your build.gradle file:

    compile 'org.nanohttpd:nanohttpd:2.2.0'
    
    1. After that, you must create an Android Web Server Class this way:
    public class AndroidWebServer extends NanoHTTPD {
        
        public AndroidWebServer(int port) {
            super(port);
        }
        
        public AndroidWebServer(String hostname, int port) {
            super(hostname, port);
        }
            
        //...
    }
    
  2. Add serve() method in your Android Web Server Class :

    @Override
    public Response serve(IHTTPSession session) {
        String msg = "<html><body><h1>Hello server</h1>\n";
        Map<String, String> parms = session.getParms();
        if (parms.get("username") == null) {
            msg += "<form action='?' method='get'>\n";
            msg += "<p>Your name: <input type='text' name='username'></p>\n";
            msg += "</form>\n";
        } else {
            msg += "<p>Hello, " + parms.get("username") + "!</p>";
        }
        return newFixedLengthResponse( msg + "</body></html>\n" );
    }
    

    serve() is a very important method beacause this is the response sent by your web server.

    1. You can now instantiate and start your server in your activity. (Full implementation here)

      AndroidWebServer androidWebServer = new AndroidWebServer(port);
      androidWebServer.start();
      
      androidWebServer.stop();
      

    LICENCE

    AndroidWebServer by Lopez Mikhael is licensed under a Apache License 2.0.