我正在开发一个Android应用程序,它使用nanohttpd创建一个webserver我的代码不会给我任何错误,但服务器没有运行,因为当我去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.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;
}
}
}
答案 0 :(得分:2)
看起来像一个简单的修复---在onResume()中创建服务器,但你仍然需要在其上调用“start()”。