java.lang.ClassCastException:java.util.Vector无法强制转换为org.ksoap2.serialization.SoapPrimitive

问题描述 投票:1回答:1

我试图将我的Android APP开发连接到Web服务,似乎无法捕获响应(抛出上述错误);我的APP代码片段如下:

@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();

我阅读了一些文章/帖子,建议将上一行更改为以下行:

Object soapPrimitive = (Object) envelope.getResponse();

虽然这确实“解决”了错误,但是服务的返回值是未命名的:

  1. 2119100
  2. 278

...而不是,在一个可用的“配对”;

response.getAttribute("AccountNumber") = 2119100
response.getAttribute("AccountBalance") = 278
response.getAttribute("StatusCode") = "E"

我确实掌握了Web服务,因此可以在那里进行编辑,如果这是解决方案;该服务的C#代码片段:

[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";
    }

以上是实际服务的显着缩减版本,其中(用于生产)将返回[例如]金融交易的完整细目,包括期初余额,当前余额,增值税,利息,折扣等,因此,跟踪未命名的索引列表将逐渐成为维护的噩梦。

[编辑22/02/2019]

我已经更改(和简化)了WebService,因此它只接受INPUT值

[WebMethod(
    Description = "Account Lookup", 
    MessageName = "Account",
    EnableSession = false)]
public string Account(
    string PostCode, 
    string Surname)

..并且从该方法中,我对处理“out”参数的私有方法进行了更深入的调用。然后WebMethod将这些参数转换为JSON字符串并返回它。这个(相当丑陋的方法)工作,直到我想返回一个数据数组,直接回到原来的错误。

...下一步(因为我找不到一个可行的解决方案)将尝试与XML响应相同。

java c# ksoap2
1个回答
1
投票

可能是envelope.getResponse()调用返回SoapPrimitives的集合。

尝试类似的东西

Vector responseVector = (Vector) envelope.getResponse();
for (Vector res : responseVector) {
    SoapPrimitive soapPrimitive = (SoapPrimitive) res;
    // your code here using soapPrimitives
}

如果元素的类型仍然不同于SoapPrimitive,那么将会有一个新的Exception,你可以相应地进行投射。

© www.soinside.com 2019 - 2024. All rights reserved.