Open agGitHub opened 2 years ago
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
Hi,
I have an Android app and I implemented nanohttpd. I can run some simple html code like this:
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.