如何在Android中创建nanohttpd服务器? [英] How to create nanohttpd server in android?
问题描述
其实,我已经搜查了一些问题,去GitHub的。但我是新的,我无法理解的例子。
我要创建在Android上的http服务器,所以我可以访问它在PC浏览器。
我有实例的类扩展nanohttpd,但服务器的不愉快。我不知道为什么,我的电脑和手机都在同一WIFI,呃......
公共类MyHTTPD扩展NanoHTTPD {
/ **
*根据给定的端口上的HTTP服务器。
* /
公共MyHTTPD()抛出IOException异常{
超(8080);
}
@覆盖
公众的反应发球(字符串URI,方法的方法,
地图<字符串,字符串>头,地图<字符串,字符串> PARMS,
地图<字符串,字符串>文件)
{
的System.out.println(方法+222+ URI +');
弦乐味精=< HTML><身体GT;< H1>你好服务器16; / H1> \ N的;
如果(parms.get(用户名)== NULL)
味精+ =
<形式的行动=?方法='得到'> \ N+
< P>您的名字:<输入类型=文本名称='用户名'>< / P> \ N+
< /形式GT; \ N的;
其他
MSG + =&其中p为H.;你好,+ parms.get(用户名)+&所述;!/ P>中;
味精+ =< /身体GT;< / HTML> \ N的;
返回新NanoHTTPD.Response(MSG);
}
公共静态无效的主要(字串[] args)
{
尝试
{
新MyHTTPD();
}
赶上(IOException异常IOE)
{
通信System.err.println(无法启动服务器:\ N+ IOE);
System.exit(-1);
}
的System.out.println(监听端口8080上按下回车键,停止\ñ。);
尝试{System.in.read(); }赶上(的Throwable T){
的System.out.println(读取错误);
};
}
}
您样品code是缺少一个小细节 - 你创建的服务器,但你永远不叫开始()方法,它踢它关闭听传入的连接。在你的main()方法,你可以写
(新MyHTTPD())启动()。
和一切都会很好,你的服务器会响应你所希望的那样的方式。
它的工作原理是这样的原因是双重的:我想要的构造是一个便宜的,廉价的操作,无副作用。例如,当单元测试,我称之为开始()中的设置的和停止()中的拆卸的我的JUnit测试方法。
Actually ,I had searched some questions and go to the github. But I'm new ,I cannot understand the example.
I want to create the http server in android so I can access it in PC browser.
I had instance a class extend nanohttpd, but the server just don't work. I don't know why ,my computer and phone are in the same WIFI,uh.....
public class MyHTTPD extends NanoHTTPD {
/**
* Constructs an HTTP server on given port.
*/
public MyHTTPD()throws IOException {
super(8080);
}
@Override
public Response serve( String uri, Method method,
Map<String, String> header, Map<String, String> parms,
Map<String, String> files )
{
System.out.println( method + " '222" + uri + "' " );
String msg = "<html><body><h1>Hello server</h1>\n";
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 );
}
public static void main( String[] args )
{
try
{
new MyHTTPD();
}
catch( IOException ioe )
{
System.err.println( "Couldn't start server:\n" + ioe );
System.exit( -1 );
}
System.out.println( "Listening on port 8080. Hit Enter to stop.\n" );
try { System.in.read(); } catch( Throwable t ) {
System.out.println("read error");
};
}
}
Your sample code is missing one small detail - you create the server but you never call the "start()" method which kicks it off to listen for incoming connections. In your main() method, you could write
(new MyHTTPD()).start();
and all would be well, your server would respond the way you hoped it would.
The reason it works that way is twofold: I want the constructor to be a cheap, inexpensive operation, without side-effects. For instance, while unit testing, I call "start()" in the setup and "stop()" in the teardown methods of my jUnit test.
这篇关于如何在Android中创建nanohttpd服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!