【0】README
0.1)本文旨在演示一个简单荔枝,以说明如何使用 NanoHTTPD web server 和 浏览器访问 该server的效果
--------------------------------------------------------------
App.java
public class App extends NanoHTTPD
{
public App() throws IOException
{
super(8080);
start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
System.out.println("\nRunning! Point your browers to http://localhost:8080/ \n");
}
public static void main(String[] args)
{
try
{
new App();
} catch (IOException ioe)
{
System.err.println("Couldn't start server:\n" + ioe);
}
}
@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");
}
}
https://github.com/NanoHttpd/nanohttpd/blob/master/core/src/main/java/fi/iki/elonen/NanoHTTPD.java
需要注意的是, 该源文件的import 列表的最后两行是需要修改的,修改的 import dir 就是你的 工作目录,
【2】最后的访问结果