在MainAvtivity.java中,我添加了以下两种方法:
@Override
protected void onStart()
{
super.onStart();
TextToSpeechServer.main(null);
}
和
@Override
protected void onStop() {
super.onStop();
}
这是TextToSpeechServer代码:
package com.adi.webservertest;
import java.util.Map;
/**
* An example of subclassing NanoHTTPD to make a custom HTTP server.
*/
public class TextToSpeechServer extends NanoHTTPD {
public TextToSpeechServer() {
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("say") != null)
msg += "<p>Hello, " + parms.get("say") + "!</p>";
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 Response(msg);
}
public static void main(String[] args) {
ServerRunner.run(TextToSpeechServer.class);
}
}
MainActivity.java onStart中的方法运行正常。 但是我应该怎样调用以及如何在onStop方法中停止服务器?