我使用以下代码通过soap调用方法。它工作得很好。
private static final String SOAP_ACTION = "http://tempuri.org/GetAuthenticateUser";
private static final String METHOD_NAME = "GetAuthenticateUser";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://stage.mysite.com/FinancialSnapshotService/Service.asmx?WSDL";
// I have tried http://stage.mysite.com/FinancialSnapshotService/Service.asmx also
public void getResults() {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("vstrUserID", "vk@gmail.com");
request.addProperty("vstrPassword", "password");
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(request);
HttpTransportSE aht = new HttpTransportSE(URL);
try {
aht.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
aht.call(SOAP_ACTION, soapEnvelope);
SoapObject result = (SoapObject) soapEnvelope.getResponse();
Log.d("WS", String.valueOf(result));
} catch (Exception e) {
e.printStackTrace();
}
}
但是当我尝试在同一服务器中的某些其他方法中使用相同的代码时,它会给出ClassCastException: org.ksoap2.SoapFault
。如果我将行SoapObject result = (SoapObject) soapEnvelope.getResponse();
更改为SoapObject result = (SoapObject)soapEnvelope.bodyIn;
,则效果非常好。任何人都可以告诉我这段代码的基本内容,在哪里使用bodyIn
以及在哪里使用getResponse()
?
答案 0 :(得分:2)
我使用以下代码来解决这个问题
try{
result = (SoapObject) soapEnvelope.getResponse();
}catch (ClassCastException e) {
result = (SoapObject)soapEnvelope.bodyIn;
}
但我仍然不清楚为什么会这样。
答案 1 :(得分:0)
如果你去那里,你会看到身体没有被序列化,而得到respose给你soapobject已经加强了。是我的承诺,如果我错了,我很抱歉。 http://ksoap2.sourceforge.net/doc/api/org/ksoap2/SoapEnvelope.html#bodyIn
我有同样的问题,因为我的网络服务正在给我html,如果是你的问题,我建议你在网络服务端编码anser。
添加try和catch
catch (SoapFault e) {
SoapObject result = soapEnvelope.BodyIn;
}