http - How to create nanohttpd server in android? -


actually ,i had searched questions , go github. i'm new ,i cannot understand example.

i want create http server in android can access in pc browser.

i had instance class extend nanohttpd, server don't work. don't know why ,my computer , phone in same wifi,uh.....

public class myhttpd extends nanohttpd {       /**      * constructs http server on given port.      */     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 stop.\n" );         try { system.in.read(); } catch( throwable t ) {             system.out.println("read error");         };     }  } 

your sample code missing 1 small detail - create server never call "start()" method kicks off listen incoming connections. in main() method, write

        (new myhttpd()).start(); 

and well, server respond way hoped would.

the reason works way twofold: want constructor cheap, inexpensive operation, without side-effects. instance, while unit testing, call "start()" in setup , "stop()" in teardown methods of junit test.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

keyboard - C++ GetAsyncKeyState alternative -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -