Actually, I searched for some questions and went to github. But I am a beginner, I can not understand an example.
I want to create an http server in android in order to access it in a PC browser.
I had an instance of a class extending nanohttpd, but the server just wasn't working. I donβt know why, my computer and phone are in the same WIFI that ...
public class MyHTTPD extends NanoHTTPD { public MyHTTPD()throws IOException { super(8080); } @Override public Response serve( String uri, Method method, Map<String, String> header, Map<String, String> parms, Map<String, 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"); }; } }
source share