I am trying to use NanoHTTP to serve up an HTML file. However, NanoHTTP is relatively un-documented, and I am new to Android. My question is, where do I store the html file, and how specifically can I serve it up using NanoHTTP.
相关问题
- Angular RxJS mergeMap types
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
Try this... Create 2 packages(activity, util), only for organization In activity create the class MainActivity.java in util create the class AndroidWebServer.java
In the AndroidWebserver.java
Don't forget Manifest.xml
And last
activity_main.xml
A late answer but may be useful to others.
Here is a simple hello Web Server, not exactly what you ask, but you can continue from here. The following program supposes you have a
www
directory in the root of the SD Card and a fileindex.html
inside.Main activity
Httpd.java
:Obviously the
NanoHTTPD
class must be in the same package.You need to grant internet permission in
AndroidManifest.xml
.and read external storage permission.
EDIT: To access the server open you web browser with the IP of your device, e.g.
192.168.1.20:8080
.NOTES:
Take a look at how I serve HTML files and other type of files too. I have a AndroidWebServer class which extends the Nanohttpd class. This is my response method -->
As you can see, I have implemented GET and POST method. You can find each in this part of the code
uri.equals("/getmethod")
anduri.equals("/getmethod")
.Also, you can see the part -->
uri.equals("/openrap")
, here I am serving a JPG file to the client's browser and the image is present in the internal directory at/www/
folder.Ping me if you have any doubts.
Pretty good source code can be found here: https://github.com/Teaonly/android-eye
Chceck assets folder where html and JavaScript files are stored https://github.com/Teaonly/android-eye/tree/master/assets
TeaServer - server implementation https://github.com/Teaonly/android-eye/blob/master/src/teaonly/droideye/TeaServer.java
MainActivity - server initialization https://github.com/Teaonly/android-eye/blob/master/src/teaonly/droideye/MainActivity.java
Updated
WebServer
class (see rendon's reply) that works with current NanoHTTPD version: