0

I want to add a http server inside the android application. I have tried NanoHTTPD server taken from https://github.com/NanoHttpd/nanohttpd. So I am able to run this http server as simple java class in my desktop . and able to access it from desktop browser.

Now I want to add this code with android application and start this server inside android device and access it from my mobile device browser.Is is possible and is there any other server i can bind with my android app .

neeraj t
  • 4,654
  • 2
  • 27
  • 30
  • 1
    It is possible. Have you tried it? You will need to run that server in a background thread though. – zapl Jan 21 '14 at 09:39
  • thanks zapl for quick response, its working now. I can access webpage at http://127.0.0.1:8080. – neeraj t Jan 21 '14 at 09:49

2 Answers2

2

This code works:

private class MyHTTPD extends NanoHTTPD {
    public MyHTTPD() throws IOException {
        super(8080);
    }

    @Override
    public Response serve(String uri, String method, Properties header, Properties parms, Properties files) {
        InputStream is = new FileInputStream("/sdcard/1.mp3");
        return new NanoHTTPD.Response(HTTP_OK, "audio/mp3", is);
    }
}

server = new MyHTTPD();
server.start();
// and now you can use http://localhost:8080 to do something (ex : streaming audio ...)
Dev2017
  • 857
  • 9
  • 31
henry4343
  • 3,871
  • 5
  • 22
  • 30
2

Here is my working sample code. It has limitation as activity will removed from memory my server will stop. To remove that I will write a service for this task.

public class MainActivity extends Activity {
     private static final int PORT = 8080;

      private MyHTTPD server;


      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       }

      @Override
      protected void onResume() {
        super.onResume();
            server = new MyHTTPD();
        try {
            server.start();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      }

      @Override
      protected void onPause() {
        super.onPause();

      }
      @Override
      protected void onDestroy() {
        super.onDestroy();
        if (server != null)
          server.stop();
      }

      private class MyHTTPD extends NanoHTTPD {
          public MyHTTPD() {
                super(8080);
            }

            @Override public Response serve(IHTTPSession session) {
                Method method = session.getMethod();
                String uri = session.getUri();
                System.out.println(method + " '" + uri + "' ");

                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>";

                msg += "</body></html>\n";

                return new NanoHTTPD.Response(msg);
            }
      }

}

//now access web page using http://127.0.0.1:8080
neeraj t
  • 4,654
  • 2
  • 27
  • 30