Question

i am developing a android application which uses nanohttpd to create a webserver my code do not give me any error but the server is not running because when i go to xx.xxx.xxx.xxx:8765/index.htm then it gives my no result this is my code: Please Help...

package dolphin.developers.com;

import java.io.File;
import java.io.IOException;
 import java.util.Properties;

import dolphin.devlopers.com.R;

import android.app.Activity;
 import android.os.Bundle;
import android.os.Environment;

     public class AlertDialogActivity extends Activity {
     private static final int PORT = 8765; 
     private MyHTTPD server;

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

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


  try {
   server = new MyHTTPD();
   } catch (IOException e) {

        e.printStackTrace();
}
}



    @Override

    protected void onPause() {

        super.onPause();

        if (server != null)
      server.stop();

    }

    public class MyHTTPD extends NanoHTTPD {

         public MyHTTPD() throws IOException {

             super(PORT, null);

         }

         public Response serve( String uri, String method, Properties header, Properties parms, Properties files ) {
                File rootsd = Environment.getExternalStorageDirectory();
                File path = new File(rootsd.getAbsolutePath() + "/samer");
                Response r = super.serveFile("/index.htm", header, path, true);
                return r;
}
}
}
Was it helpful?

Solution

Looks like a simple fix --- in onResume() you create the server but you still need to call "start()" on it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top