gpt4 book ai didi

java - 调用 Web 服务方法时无法转换对象错误

转载 作者:行者123 更新时间:2023-12-01 13:15:56 25 4
gpt4 key购买 nike

我试图使用 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 的值传递 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);

希望这能澄清。

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

关于java - 调用 Web 服务方法时无法转换对象错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22472214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com