MonoDroid 简单 Web 服务器
我创建了一个在 Android 设备上运行的简单 Web 服务器,该服务器基本上以 Hello World 进行响应,该服务器在 Preview 11 之前可以正常工作。是否有任何重大更改导致该服务器不再工作,或者我做错了什么?应用程序在 context.Response.OutputStream.Write( buffer, 0, buffer.Length ); 上崩溃
代码:
public class Activity1 : Activity
{
private HttpListener listener;
protected override void OnCreate( Bundle bundle )
{
base.OnCreate( bundle );
// Set our view from the "main" layout resource
SetContentView( Resource.Layout.Main );
// Get our button from the layout resource,
// and attach an event to it
var button = FindViewById<Button>( Resource.Id.MyButton );
button.Click += button_Click;
}
private void button_Click( object sender, EventArgs e )
{
try
{
if ( !HttpListener.IsSupported )
return;
listener = new HttpListener();
listener.Prefixes.Add( "http://+:8001/" );
listener.Start();
listener.BeginGetContext( HandleRequest, listener );
}
catch ( Exception )
{
throw;
}
}
private void HandleRequest( IAsyncResult result )
{
HttpListenerContext context = listener.EndGetContext( result );
string response = "<html>Hello World</html>";
byte [] buffer = Encoding.UTF8.GetBytes( response );
context.Response.ContentLength64 = buffer.Length;
context.Response.OutputStream.Write( buffer, 0, buffer.Length );
context.Response.OutputStream.Close();
listener.BeginGetContext( HandleRequest, listener );
}
}
日志:
I/ActivityManager(112):进程torqsoftware.testwebserver(pid 3044)已死亡。
I/WindowManager(112):胜利死亡:窗口{44d15120 torqsoftware.testwebserver/monodroidwebservertest.Activity1暂停= false}
D/Zygote(58):进程3044被信号终止(4)
V/RenderScript_jni(199):surfaceCreated
V/RenderScript_jni (199):surfaceChanged
I/UsageStats(112):com.android.launcher意外恢复,而已经在torqsoftware.testwebserver中恢复
W/InputManagerService(112):收到RemoteException发送setActive(false)通知到pid 3044 uid 10062
谢谢 狮子座
I've created a simple web server that runs on the Android device that basically response with Hello World that used to work prior to Preview 11. Was there any major change that causes this to no longer work or am I doing something wrong? The application crashes on context.Response.OutputStream.Write( buffer, 0, buffer.Length );
Code:
public class Activity1 : Activity
{
private HttpListener listener;
protected override void OnCreate( Bundle bundle )
{
base.OnCreate( bundle );
// Set our view from the "main" layout resource
SetContentView( Resource.Layout.Main );
// Get our button from the layout resource,
// and attach an event to it
var button = FindViewById<Button>( Resource.Id.MyButton );
button.Click += button_Click;
}
private void button_Click( object sender, EventArgs e )
{
try
{
if ( !HttpListener.IsSupported )
return;
listener = new HttpListener();
listener.Prefixes.Add( "http://+:8001/" );
listener.Start();
listener.BeginGetContext( HandleRequest, listener );
}
catch ( Exception )
{
throw;
}
}
private void HandleRequest( IAsyncResult result )
{
HttpListenerContext context = listener.EndGetContext( result );
string response = "<html>Hello World</html>";
byte [] buffer = Encoding.UTF8.GetBytes( response );
context.Response.ContentLength64 = buffer.Length;
context.Response.OutputStream.Write( buffer, 0, buffer.Length );
context.Response.OutputStream.Close();
listener.BeginGetContext( HandleRequest, listener );
}
}
log:
I/ActivityManager( 112): Process torqsoftware.testwebserver (pid 3044) has died.
I/WindowManager( 112): WIN DEATH: Window{44d15120 torqsoftware.testwebserver/monodroidwebservertest.Activity1 paused=false}
D/Zygote ( 58): Process 3044 terminated by signal (4)
V/RenderScript_jni( 199): surfaceCreated
V/RenderScript_jni( 199): surfaceChanged
I/UsageStats( 112): Unexpected resume of com.android.launcher while already resumed in torqsoftware.testwebserver
W/InputManagerService( 112): Got RemoteException sending setActive(false) notification to pid 3044 uid 10062
Thanks
Leo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这似乎与当用户移动到 Android 中的另一个应用程序时 Activity 被“暂停”这一事实有关。在这种情况下,您可能已打开浏览器来访问在暂停的活动中运行的 http 服务器。我建议您将 http 服务器实现为服务而不是活动。
It seems to be related to the fact that an Activity is "paused" when user moves on to another application in android. As in this case you may have opened browser to access the http server which is running in the paused activity. I would suggest you to implement the http server as a Service and not as an activity.