Set up HttpServer - NanoHttpd
NanoHttp is a light-weight HTTP server designed for embedding system, released under a BSD licence.
Step 1: Download .jar
Step 2: Import .jar
Unzip file and copy .jar file to your project/libs folder.
Step 3: Add info in build.gradle
1 | android { |
Step 4: Create custom class
Create a custom class and extends NanoHTTPD.
1 | public class NanoHttpServer extends NanoHTTPD { |
Step 5: Create NanoHttpServer
Created NanoHttpServer and start http server and set up a custom port to communicate with client.
1 | mHttpServer = new NanoHttpServer(6600); |
Step 6: Receive message from client
When client is connected, we can receive the message and return response to client.
1 |
|
It should be noted that IHTTPSession.getInputStream will never return -1 as end of stream.
But we could use a buffer to control loop. When the length of reading stream less than buffer size, than we break this loop.
1 | try { |
Set up HttpServer - NanoHttpd
https://nickcarter9.github.io/2019/04/01/2019/2019_04_01-nanohttpd/