【问题标题】:Cannot cast object error while invoke web service method调用 Web 服务方法时无法转换对象错误
【发布时间】:2014-03-18 06:50:40
【问题描述】:

我试图使用 Web 服务 [http://www.webservicex.net/ConvertSpeed.asmx?WSDL] 并在其中调用一个方法。

它只有一种称为 SpeedUnit 的方法,并尝试将其与自定义参数一起使用。

只有当我给出下面的代码时它才有效

double res = serProxy.convertSpeed(spd,SpeedUnit.milesPerhour,
            SpeedUnit.kilometersPerhour);

但我想像下面这样管理第二个和第三个参数。

    double spd = 100;  
    SpeedUnit callTunit = "SpeedUnit."+toUnit;
    SpeedUnit callFunit = "SpeedUnit."+fromUnit;
    double res = serProxy.convertSpeed(spd,callFunit,
            callTunit);

我得到下面的转换异常错误消息

“无法将具有类‘java.lang.String’的对象‘SpeedUnit.kilometersPerhour’转换为类‘NET.webserviceX.www.SpeedUnit’”

在 SpeedUnit 类中,这些对象声明如下。

public class SpeedUnit implements java.io.Serializable {
private java.lang.String _value_;
private static java.util.HashMap _table_ = new java.util.HashMap();protected SpeedUnit(java.lang.String value) {
    _value_ = value;
    _table_.put(_value_,this);
}
public static final java.lang.String _milesPerhour = "milesPerhour";
public static final java.lang.String _kilometersPerhour = "kilometersPerhour";
public static final SpeedUnit milesPerhour = new SpeedUnit(_milesPerhour);
public static final SpeedUnit kilometersPerhour = new SpeedUnit(_kilometersPerhour);

非常感谢任何人的帮助。

谢谢, 马丹

【问题讨论】:

  • toUnitfromUnit 数据类型是什么?他们是字符串吗?请同时给出真实值。
  • toUnit 和 fromUnit 是字符串数据类型,它分别保存值milesPerhour 和 kmPerhour
  • 我已经发布了答案。请检查这是否对您有帮助。

标签: java web-services groovy


【解决方案1】:

我认为您需要根据toUnitfromUnit 的值传递SpeedUnit 的新实例。我完全假设 toUnitfromUnitString 数据类型,因为您已经完成了连接。如果我的假设是正确的,您可以将代码更改为

SpeedUnit fromSUUnit = null;
SpeedUnit toSUUnit = null;
if(fromUnit !=null && fromUnit.equals("milesPerhour"))
fromSUUnit = SpeedUnit.milesPerhour;
if(toUnit !=null && toUnit.equals("kilometersPerhour"))
toSUUnit = SpeedUnit.kilometersPerhour;
double res = serProxy.convertSpeed(spd,fromSUUnit,toSUUnit);

希望这可以澄清。

免责声明:以上代码未经测试。只是输入它来提供逻辑

【讨论】:

    【解决方案2】:

    webservice 包含字符串值,并且您正在向 webservice 发送一个 SpeedUnit 对象。只需按原样传递字符串值。 serProxy.convertSpeed(spd,toUnit, 从单位);

    【讨论】:

    • 我像你说的那样改变了,我得到了错误 NoMethod Exception
    • 错误消息:`没有方法签名:NET.webserviceX.www.ConvertSpeedsSoapProxy.convertSpeed() 适用于参数类型:(java.lang.Double, java.lang.String, java.lang .String) 值:[100.0, SpeedUnit.kilometersPerhour, SpeedUnit.milesPerhour]`
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 2015-05-20
    • 1970-01-01
    • 2016-08-03
    相关资源
    最近更新 更多