Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nanohttpd Android: how to run a complex website on local server? #619

Open
agGitHub opened this issue Dec 2, 2021 · 2 comments
Open

nanohttpd Android: how to run a complex website on local server? #619

agGitHub opened this issue Dec 2, 2021 · 2 comments

Comments

@agGitHub
Copy link

agGitHub commented Dec 2, 2021

Hi,

I have an Android app and I implemented nanohttpd. I can run some simple html code like this:

    @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  <p>Your name: <input type='text' name='username'></p>\n" + "</form>\n";
        } else {
            msg += "<p>Hello, " + parms.get("username") + "!</p>";
        }
        return newFixedLengthResponse( msg + "</body></html>\n" );
    }

But what I want is to run a full complex website with an index.html file and many other files (namely javascript files). To be more specific, I want to run a WebGL website on a local server on my Android phone.

So, I have all the files of my website in the 'assets' folder of my apk. When the app launches, I copy all the files to the internal storage of the Android phone (to file:///storage/emulated/0/android/data/com.example.mysuperapp/files )

index.html is in /files.

How can I do if I want that http://192.168.0.26:8080/index.html runs smoothly the website located at file:///storage/emulated/0/android/data/com.example.mysuperapp/files/index.html ? (meaning that all the javascript files, imports, etc... should work properly)

If nanohttpd is not designed for that, is there any other tool to do that?

Thanks a lot for your help.

@yvelltt
Copy link

yvelltt commented Jan 19, 2022

set the mimetype, and put your file into assets

val uri = session.uri
var filename = uri.substring(1)
var mimetype = "text/html"
if (uri == "/") filename = "index.html"
if (filename.contains(".html") || filename.contains(".htm")) {
    mimetype = "text/html"
    return stringResponse(filename, mimetype)
} else if (filename.contains(".js")) {
    mimetype = "text/javascript"
    return stringResponse(filename, mimetype)
} else if (filename.contains(".css")) {
    mimetype = "text/css"
    return stringResponse(filename, mimetype)
} else if (filename.contains(".jpeg") || filename.contains(".jpg")) {
    mimetype = "image/jpeg"
    return fileResponse(filename, mimetype)
} else if (filename.contains(".png")) {
    mimetype = "image/png"
    return fileResponse(filename, mimetype)
} else if (filename.contains(".gif")) {
    mimetype = "image/gif"
    return fileResponse(filename, mimetype)
} else if (filename.contains(".ico")) {
    mimetype = "image/x-icon"
    return fileResponse(filename, mimetype)
} else if (filename.contains(".woff2")) {
    mimetype = "application/font-woff2"
    return fileResponse(filename, mimetype)
} else if (filename.contains(".woff")) {
    mimetype = "application/font-woff"
    return fileResponse(filename, mimetype)
} else if (filename.contains(".ttf")) {
    mimetype = "application/octet-stream"
    return fileResponse(filename, mimetype)
} else if (filename.contains(".eot")) {
    mimetype = "application/vnd.ms-fontobject"
    return fileResponse(filename, mimetype)
} else {
    filename = "index.html"
    mimetype = "text/html"
    return stringResponse(filename, mimetype)
}

private fun stringResponse(filename : String, mimetype : String): Response? {
      var response: String? = ""
      var line: String? = ""
      var reader: BufferedReader? = null
      try {
          reader = BufferedReader(InputStreamReader(application.assets.open("HCWeb/$filename")))
          while (reader.readLine().also { line = it } != null) {
              response += line
          }
          reader.close()
      } catch (e: IOException) {
          e.printStackTrace()
      }
      return newFixedLengthResponse(Response.Status.OK, mimetype, response)
  }

  private fun fileResponse(filename : String, mimetype : String): Response? {
      var inputFile : InputStream? = null
      var fileSize : Long = 0
      try {
          val fd = assets.openFd("test.png")
          fileSize = fd.length
          Log.i("fileSize", fileSize.toString())
          inputFile = application.assets.open("HCWeb/$filename")
      } catch (e: IOException) {
          e.printStackTrace()
      }
      return newFixedLengthResponse(Response.Status.OK, mimetype, inputFile, fileSize )
  }

and you can put your file like this picture

t

@tejmagar
Copy link

Try this NanoHttpd Wrapper library created by me
TinyWeb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants