情况是这样的:我有一个正在运行的 WCF 服务,它在 C# 中与此方法通信:
public bool ValidateUser(UserPass up)
{
initializeAttributes();
IMembershipService Member = new AccountMembershipService();
bool login = Member.ValidateUser(up.User, up.Pass);
return login;
}
参数封装在这个类中:
[DataContract]
public class UserPass
{
string user = "";
string pass = "";
string email = "";
[DataMember]
public string User
{
get { return user; }
set { user = value; }
}
[DataMember]
public string Pass
{
get { return pass; }
set { pass = value; }
}
[DataMember]
public string Email
{
get { return email; }
set { email = value; }
}
}
现在,我想通过 Android 应用程序连接到服务器,现在,我的问题是,我如何在 Java 中复制 UserPass 类,以便 ValidateUser 方法能够以它可以理解的方式接收它的参数。
作为引用,这是我获取用户和密码的代码:
private void validateUser(String user, String pass)
{
String SOAP_ACTION = "http://tempuri.org/IUserService/ValidateUser/";
String METHOD_NAME = "ValidateUser";
String NAMESPACE = "http://tempuri.org/";
String URL = "http://10.0.2.2/UserService.svc";
AlertDialog popup;
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty(user, pass);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = request;
envelope.dotNet = true;
HttpTransportSE httpTransport = new HttpTransportSE(URL);
try
{
httpTransport.call(SOAP_ACTION, envelope); //here's the exception!!
Object response = envelope.getResponse();
popup = createAlertDialog("Respuesta",response.toString(),"OK");
popup.show();
}
catch (Exception exception)
{
String exceptionStr=exception.toString();
popup = createAlertDialog("Exception!!",exceptionStr,"OK");
popup.show();
}
}
它抛出的异常是xmlpullparserexception,按照我的理解是因为请求的参数和实际的方法不匹配。
非常感谢阅读我的问题,对于那些能够回答问题的人还有更多:)
编辑:
我终于知道了如何比较 XML...现在,这就是我的 SOAP 所提供的:
<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns:d="http://www.w3.org/2001/XMLSchema"
xmlns:c="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
<v:Header />
<v:Body>
<ValidateUser xmlns="http://tempuri.org/" id="o0" c:root="1">
<User i:type="d:string">someuser</User>
<Pass i:type="d:string">somepass</Pass>
<Email i:type="d:string"></Email>
</ValidateUser>
</v:Body>
这是它应该做的(从 Visual Studio 2010 的 WCF 测试客户端应用程序中检索):
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IUserService/ValidateUser</Action>
</s:Header>
<s:Body>
<ValidateUser xmlns="http://tempuri.org/">
<up xmlns:d4p1="http://schemas.datacontract.org/2004/07/LiveAndesWCF" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<d4p1:Email i:nil="true" />
<d4p1:Pass>somepass</d4p1:Pass>
<d4p1:User>someuser</d4p1:User>
</up>
</ValidateUser>
</s:Body>
</s:Envelope>
现在,我不知道如何编写我的 soap 代码以使其生成像后者那样的 xml 文件。
再次感谢。
最佳答案
您是否尝试过查看 soap 调用创建的 xml?您可以将它与 .net 代理创建的 xml 进行比较。也许这有助于找到解决方案。
以下是启用 soap 调用记录的方法: http://msdn.microsoft.com/en-us/library/ms730064.aspx
关于c# - 无法以与 C# 相同的格式为 WCF 服务传递 java 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7374779/