我目前正在使用 Mono for Android 开发移动应用程序。我一直在尝试通过向其添加 Web 引用来使用 WCF Web 服务,但我似乎无法以这种方式进行调用。我现在正在考虑咬紧牙关,使用我不擅长的 Java 重写代码,因为我不擅长 C#。
我有两个问题:
- 如何使用 Mono for android 使用 WCF 网络服务。
如果我要使用 java,我将如何调用如下所示的方法:
[OperationContract] [WebInvoke(Method = "POST", UriTemplate = "/MyMethod", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] void MyMethod(CustomObjectFromDataContract c_Object);
当我进行调用时,我收到一个 MessageBox,其中显示未处理的异常 System.Net.WebException:。当我进入代码时,我发现错误发生在您调用
[System.Web.Services.Protocols.SoapDocumentMethodAttribute ("http://tempuri.org/IMyService/MyMethod", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
public void MyMethod([System.Xml.Serialization.XmlElementAttribute(IsNullable=true)] CustomObjectFromDataContract c_Object) {
this.Invoke("MyMethod", new object[] {
c_Object});
}
调用是抛出异常的那个。
最佳答案
我已经解决了这个问题。以下是完成这项工作的步骤: 1.服务必须是RESTful服务 2. 而不是引用 Localhost(我在生成的代码中)使用主机的 IP 地址。我认为这是因为 Android 在 Dalvik VM 中运行,我怀疑它与我的开发计算机正在使用的本地主机不同。 我这样做了,我的服务现在可以正常工作了。
关于c# - 用于 Android 或 Java 和 WCF 的单声道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9167397/