java.lang.ClassCastException:java.util.Vector无法转换为org.ksoap2.serialization.SoapPrimitive [英] java.lang.ClassCastException: java.util.Vector cannot be cast to org.ksoap2.serialization.SoapPrimitive
问题描述
我正在尝试将我的Android APP开发连接到Web服务,但似乎无法捕获响应(引发上面的错误);我的APP代码的片段如下:
I am trying to connect my Android APP development to a Web Service and cannot seem to capture the response (throws the above error); the snippet of my APP code, follows:
@Override
protected String doInBackground(String... params) {
String URL = "https://MyDomain/Services/AccountManager.asmx";
String NAMESPACE = "http://tempuri.org/";
String METHOD_NAME = parameters.get("METHOD_NAME");
String ReturnValue;
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
// addProperty for each KeyPair in the Parameters HashMap
for (Map.Entry<String, String> parameter : parameters.entrySet()) {
if (!parameter.getKey().startsWith("_") && parameter.getKey() != "METHOD_NAME") {
request.addProperty(parameter.getKey() , parameter.getValue());
}
}
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.implicitTypes = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransportSE = new HttpTransportSE(URL);
try {
httpTransportSE.call(NAMESPACE + METHOD_NAME, envelope);
SoapPrimitive soapPrimitive = (SoapPrimitive) envelope.getResponse();
ReturnValue = soapPrimitive.getAttribute("StatusCode").toString();
} catch (Exception ex) {
ReturnValue = "Error: " + ex.getMessage();
}
return ReturnValue;
}
该错误由行引发
SoapPrimitive soapPrimitive = (SoapPrimitive) envelope.getResponse();
我阅读了许多文章/帖子,建议将上一行更改为下一行:
I read a number of articles/posts which suggests changing the previous line, to the following line:
Object soapPrimitive = (Object) envelope.getResponse();
尽管这样做可以解决"错误,但是服务的返回值却未命名:
Whilst this does "workaround" the error, the return values from the service are then unnamed:
- 确定
- 2119100
- 278
- E
...而不是处于可用的配对"状态; </p>
... rather than, being in a usable "pairing";
response.getAttribute("AccountNumber") = 2119100
response.getAttribute("AccountBalance") = 278
response.getAttribute("StatusCode") = "E"
我确实可以控制Web服务,因此如果可以的话,可以在那里进行编辑.服务的C#代码段:
I do have control of the Web Service, so it's possible to make edits there, if that's the solution; a snippet of the C# code for the service:
[WebMethod(
Description = "Account Lookup",
MessageName = "Account",
EnableSession = false)]
public string Account(
string PostCode,
string Surname,
out double? AccountBalance,
out string AccountNumber,
out string StatusCode)
{
// MY STUFF HERE
AccountBalance = 50;
AccountNumber = 123456;
StatusCode = "A";
Return "OK";
}
以上是实际服务的大幅缩减版本,(用于生产)该服务将返回[例如]金融交易的完整明细,包括期初余额,当期余额,增值税,利息,折扣,等,因此跟踪未命名的索引列表将逐渐成为维护的噩梦.
The above is a significantly cut-down version of the actual service, which (for production) would return [for example] a complete breakdown of a financial transaction, with the opening balance, current balance, VAT, interest, discounts, etc., so keeping track of an unnamed indexed list will gradually become a maintenance nightmare.
我已经更改(并简化了)WebService,使其仅使用INPUT值
I have changed (and simplified)the WebService so that it ONLY takes the INPUT values
[WebMethod(
Description = "Account Lookup",
MessageName = "Account",
EnableSession = false)]
public string Account(
string PostCode,
string Surname)
..并从该方法中更深入地调用处理输出"参数的私有方法.然后,WebMethod将这些参数转换为JSON字符串并返回.这个方法(相当丑陋的方法)奏效了,直到我想返回一个数据数组,然后直接回到原始错误.
.. and from within that method, I make the more in-depth call to a private method that handled the "out" parameters. The WebMethod then turns those parameters into a JSON string and return it. This (rather ugly method) worked, until I wanted to return an array of data, which dropped straight back to the original error.
...下一步(由于无法找到有效的解决方案)将尝试与XML响应相同.
... next step (as I cannot locate a working solution) will be to try the same with and XML response.
推荐答案
可能是envelope.getResponse()
调用返回了SoapPrimitive
s的集合.
It could be that envelope.getResponse()
call returns a collection of SoapPrimitive
s.
尝试类似
Vector responseVector = (Vector) envelope.getResponse();
for (Vector res : responseVector) {
SoapPrimitive soapPrimitive = (SoapPrimitive) res;
// your code here using soapPrimitives
}
如果元素的类型仍然不同于SoapPrimitive
,则将出现一个新的Exception
,您可以进行相应的投射.
If the type of elements is still different than SoapPrimitive
, there will be a new Exception
and you can cast accordingly.
这篇关于java.lang.ClassCastException:java.util.Vector无法转换为org.ksoap2.serialization.SoapPrimitive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!