实际上,我搜索了一些问题然后去了
github.但我是新人,我无法理解这个例子.
我想在android中创建http服务器,所以我可以在PC浏览器中访问它.
我有一个类扩展nanohttpd实例,但服务器不起作用.我不知道为什么,我的电脑和手机都在同一个WIFI,呃……
public class MyHTTPD extends NanoHTTPD { /** * Constructs an HTTP server on given port. */ public MyHTTPD()throws IOException { super(8080); } @Override public Response serve( String uri,Method method,Map<String,String> header,String> parms,String> files ) { System.out.println( method + " '222" + uri + "' " ); String msg = "<html><body><h1>Hello server</h1>\n"; 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>"; msg += "</body></html>\n"; return new NanoHTTPD.Response(msg ); } public static void main( String[] args ) { try { new MyHTTPD(); } catch( IOException ioe ) { System.err.println( "Couldn't start server:\n" + ioe ); System.exit( -1 ); } System.out.println( "Listening on port 8080. Hit Enter to stop.\n" ); try { System.in.read(); } catch( Throwable t ) { System.out.println("read error"); }; } }