0

I'm using NanoHTTPD to host a web page locally from Android.

My problem is that the server responds with the index page of my site but I can't figure out how to navigate to any pages past that because it will always respond with the index page.

Anybody got any tutorials, I'm struggling to find anything. Thanks :)

private class MyHTTPD extends NanoHTTPD {
    public MyHTTPD() throws IOException {
        super(PORT, null);
    }

    @Override
    public Response serve(String uri, String method, Properties header, Properties parms, Properties files) {
        Log.d("response", "URI:" + uri + " method: " + method + " header: " + header + " parms: " + parms + " files: " + files);
        final StringBuilder buf = new StringBuilder();
        for (Entry<Object, Object> kv : header.entrySet())
            buf.append(kv.getKey() + " : " + kv.getValue() + "\n");
        handler.post(new Runnable() {
            @Override
            public void run() {
                hello.setText(buf);
            }
        });
         //load the index page
        String html = null;
        InputStream is = getClass().getResourceAsStream("/com/me/pages/index.html");
        byte[] b;
        try {
            b = new byte[is.available()];
            is.read(b);
            html = new String(b);
        } catch (IOException e) { // TODO Auto-generated catch block
            e.printStackTrace();
        }

                  //return index as response
        return new NanoHTTPD.Response(HTTP_OK, MIME_HTML, html);
    }
}
Stephan
  • 41,764
  • 65
  • 238
  • 329
user1056798
  • 245
  • 6
  • 20

1 Answers1

0

NanoHTTPD not showing other pages

Of course, it won't show the others, as your petit serve function just serves the /com/me/pages/index.html file. For the other files (pages as you say), one needs to map the URI to the corresponding file to be served.

For examples, you could follow the development of the NanoHTTPD on github by different programmers. The network graph is here, and one particular fork which could be helpful to your case (serving multiple pages) is here.

PCoder
  • 2,165
  • 3
  • 23
  • 32
  • To update those links, NanoHttpd is now found at https://github.com/NanoHttpd/nanohttpd - the network of different people who're experimenting with it is at https://github.com/NanoHttpd/nanohttpd/network - the "Simple Web Server" sample might be a good case study for you regarding serving multiple files. That can be found at https://github.com/NanoHttpd/nanohttpd/blob/master/webserver/src/main/java/fi/iki/elonen/SimpleWebServer.java – Paul Hawke Sep 06 '13 at 12:32