我已经创建了一个在Android设备上运行的简单Web服务器,该服务器基本上响应了以前在预览11之前工作的Hello World。是否有任何重大更改导致它不再起作用或我做错了什么?应用程序崩溃在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):WIN DEATH:Window {44d15120 torqsoftware.testwebserver / monodroidwebservertest.Activity1 paused = 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向pid 3044发送setActive(false)通知uid 10062
由于 利奥
答案 0 :(得分:3)
当用户移动到android中的另一个应用程序时,似乎与“暂停”这一事实有关。在这种情况下,您可能已打开浏览器以访问在暂停活动中运行的http服务器。我建议你将http服务器实现为服务而不是活动。