Java android中的Nanohttpd不提供文件
我正在开发一个android应用程序,它使用nanohttpd创建一个Web服务器。我的代码没有给我任何错误,但服务器没有运行,因为当我转到xx.xxx.xxx.xxx:8765/index.htm时,它没有给我任何结果。这是我的代码: 请帮忙Java android中的Nanohttpd不提供文件,java,android,webserver,nanohttpd,Java,Android,Webserver,Nanohttpd,我正在开发一个android应用程序,它使用nanohttpd创建一个Web服务器。我的代码没有给我任何错误,但服务器没有运行,因为当我转到xx.xxx.xxx.xxx:8765/index.htm时,它没有给我任何结果。这是我的代码: 请帮忙 package dolphin.developers.com; import java.io.File; import java.io.IOException; import java.util.Properties; import dolphin
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;
}
}
}
看起来像是一个简单的修复——在onResume()中,您创建了服务器,但仍然需要对其调用“start()”