0

I try to run a server from my android app with nanoHTTP. My aim is that everybody connected to the same network as the app can access a html webpage.

So far this is my NanoHttp-server-class:

private class MyHTTPD extends NanoHTTPD {

public MyHTTPD() throws IOException {
  super(8080);
}

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

After I start the server from the main activityn, I can see the text "Hello server", when I open ip:port in a browser.

Now my question is, how I can change the html-code from the main activity. I thought about passing the html code to the serve() Method through the IHTTPSession session, but I don't no how to do that and which parameter of session.

So my question is, how can I update the response of my nanoHTTP server or how can I call the serve() Method from the main activity?

Luis
  • 71
  • 9

1 Answers1

0

You cannot be able to call serve() method from the activity manually.

serve() method will get executed whenever the client request comes. One way you can achieve is, create one parameterised constructor which accepts String data in MyHTTPD and create the object with this constructor. Pass HTML data while creating the object. Now for every request serve that data.

Eg :

private class MyHTTPD extends NanoHTTPD {

private String htmlData;

public MyHTTPD() throws IOException {
  super(8080);
}

public MyHTTPD(String data) throws IOException {
  this();
  htmlData = data;

}

@Override
    public Response serve(IHTTPSession session) {
        // String msg = "<html><body><h1>Hello server</h1></body></html>\n";
        return newFixedLengthResponse(htmlData);
    }
Abilash
  • 218
  • 3
  • 10
  • Okay I found the solution. I create a method to change the String htmlData and call that every time the html changes. Your suggestion helped me to find the solution. – Luis Feb 13 '17 at 22:11