Android HTTP server with NanoHTTPD

###Introduction In this post I will show you how to create a http server inside your Android application. You might ask yourself why would you ever create a web server inside your Android app which in most cases should serve as a client and not as a server. Well, one use case that I have encountered would be the one where we have used a robot arm to test our payment application together with a payment terminal. In this case the robot arm would operate on the terminal and would also send REST calls to the application to send and receive data. This would be one case, I can imagine there could be many other applications of creating a http server inside your Android application.

###The start First let’s create a new Android project. I will use Gradle to build it but there are no dependencies. We could use nanoHTTPD as a JAR file but the whole idea behind it is that it is very light so the only thing we need to do is to copy and paste the class NanoHTTPD.java found in the /core folder in the GitHub repository. You can find the NanoHTTPD repository here .

###Create the server In the /src/main/java/your_package_name folder let’s create a package called server. Inside this package let’s create a class called MyServer.java. Also copy and paste NanoHTTPD.java from GitHub to this folder. Use the code bellow in order to create it.

import java.io.IOException;

public class MyServer extends NanoHTTPD {
    private final static int PORT = 8080;

    public MyServer() throws IOException {
        super(PORT);
        start();
        System.out.println( "\nRunning! Point your browers to http://localhost:8080/ \n" );
    }

    @Override
    public Response serve(IHTTPSession session) {
        String msg = "<html><body><h1>Hello server</h1>\n";
        msg += "<p>We serve " + session.getUri() + " !</p>";
        return newFixedLengthResponse( msg + "</body></html>\n" );
    }
}

Now that we have the server created let’s modify MainActivity.java. First let’s add a global variable.

private MyServer server;

Next we will add the following code to the onResume() method.

@Override
public void onResume() {
    super.onResume();
    try {
        server = new MyServer();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

I suggest you also close the server on the onPause() method. For the purpose of this demo I will not use it but please find bellow the implementation of the onPause() method.

@Override
public void onPause() {
    super.onPause();
    if(server != null) {
        server.stop();
    }
}

Before running the app make sure that inside your AndroidManifest.xml you have the following permissions:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

After all the changes have been made we can run this demo. If you run the demo on a real device then make sure you are connected to the same wifi network as your development machine. Then, after the app starts, open the browser on your development machine and type http://your-phone-or-tablet-ip:8080/HELLO_WORLD and you should see the following:

HELLO SERVER

We serve /HELLO_WORLD ! </b>

In order to find your phone or tablet ip go to WiFi Setting and then to Advanced and there you should find the ip. If you run on your emulator then open a browser window inside the emulator and type: http://localhost:8080/HELLO_WORLD and you should see the same as above.

So, now our server will respond to any call of the the device IP and the 8080 port. You can use any URI which then you can parse and transform our small server into a full fledged REST server. IHTTPSession handles one session, i.e. parses the HTTP request and returns the response. You can also get the list of parameters by using session.getParms(). This will return a Map<String, String> with all the request parameters.

You can find the full project on GitHub here.