1

I am trying to create a local web server using NanoHTTPD on Android. I know how to use it for a single file doing something like:

@Override
public Response serve(IHTTPSession session) {
    String answer = "";
    try {
        FileReader index = new FileReader(fileLocation);
        BufferedReader reader = new BufferedReader(index);
        String line = "";
        while ((line = reader.readLine()) != null) {
            answer += line;
        }
        reader.close();
    } catch(IOException ioe) {
        Log.w("Httpd", ioe.toString());
    }
    return newFixedLengthResponse(answer);
}

and setting it to port 8080 so I can browse to http://localhost:8080 and view the file. However, I would like to use NanoHTTPD to put an entire directory on the server and not just a single file, in order to run an HTML5 game. Kinda similarly to how I can browse to a folder in Command Prompt on Windows and use

python -m http.server 4444

in order to browse through the folder as if it were a website at http://localhost:4444. I'm not sure how to go about it using NanoHTTPD. Can someone help me figure out where to start, or is there another library out there that's much easier to use?

Thanks in advance. Any help would be greatly appreciated.

1 Answers1

0

You are on the right path. Quick answer look here.

What you shod do is generate HTML files of directory structure to the user. Meaning when the user is first contacting you read the local directory that you determine as root directory and create an HTML file as a response with the file names as links.

If the user then clicks on a single file serve it like you already do.

I found an answer demonstrating how to generate HTML files dynamically that you can use here.

Itamar Kerbel
  • 2,508
  • 1
  • 22
  • 29