2012-08-13 176 views
0

如何使用Mono For Android创建一个webservice?似乎所有内容都是关于使用Web服务的,而不是真正要创建一个。创建一个web服务

我已经使用这个尝试:http://www.mono-project.com/Writing_a_WebService

但System.Web.Services.WebService不存在。 System.ServiceModel还没有被翻译。有没有人有关于如何在Mono For Android上创建Web服务的线索?

感谢

回答

0

我现在试图执行下面的代码,并试图在模拟器中运行它,但我让无论是通过浏览器或通过REST客户端的请求,永远不会到达的handleRequest。

protected override void OnCreate(Bundle bundle) { 
    base.OnCreate(bundle); 

    // Set our view from the "main" layout resource 
    SetContentView(Resource.Layout.Main); 

    var startBtn = FindViewById<Button>(Resource.Id.StartBtn); 
    stopBtn.Clickable = false; 

    startBtn.Click += SetupListener; 
} 

private void SetupListener(object sender, EventArgs e) { 
    _httpListener = new HttpListener(); 
    _httpListener.Prefixes.Add("http://*:9876/"); 
    _httpListener.Start(); 
    _httpListener.BeginGetContext(HandleRequest, _httpListener); 
} 

private void HandleRequest(IAsyncResult result) { 
    var context = _httpListener.EndGetContext(result); 
    var response = "<html>Hello World</html>"; 
    var buffer = Encoding.UTF8.GetBytes(response); 

    context.Response.ContentLength64 = buffer.Length; 
    context.Response.OutputStream.Write(buffer, 0, buffer.Length); 
    context.Response.OutputStream.Close(); 

    _httpListener.BeginGetContext(HandleRequest, _httpListener); 
} 

我试图做要求如以下内容:http://本地主机:9876 /,HTTP:// 10.1.1.190:9876/和http:// 10.0.2.2:9876/但他们都不实际上涉及到应用程序。

+0

我现在已经创建了一个Windows控制台应用程序来测试HttpListener。但我无法连接。连接时出现SocketException: 由于目标机器主动拒绝,无法建立连接 我试图在http:// localhost:9876进行连接,但似乎没有通过请求模拟器和应用程序本身。 – madebysoren 2012-08-15 07:12:51

+0

我发现我需要将使用telnet客户端的端口从本地机器重定向到模拟器。然后一切正常。 – madebysoren 2012-08-20 11:29:27