Java Casting崩溃websocket
所以我有一个棘手的问题。 我正在连接websocket
随着数据的流入,一切都很好
@Override
public void onMessage(WebSocket webSocket, String text)
{
// My data looks like
// {"Type":3, "F":[1,2,3974.909912109375,27500,1639207185]}
obj = new JSONObject(text);
// Then I get the array in "F" key
o = obj.getJSONArray("F");
// I want to now cast these variables into variables to use.
// So I do...
Integer v = (Integer) o.get(0);
Integer t = (Integer) o.get(1);
// This works fine.
// If I stop here.....
// the websocket stays connected, and keeps streaming....
// However.... if I do this....
Double p = (Double) o.get(2);
// The websocket crashes, and disconnects??
// Program continues running though and there is no exceptions.
// Its merely disconnecting the socket for some reason, by casting?
}
这是怎么回事?? 为什么我不能把它换成双人的
我也试过浮球,但运气不好
有什么想法吗
伊娃也试过
Double p = new Double((Integer) o.get(2));
Double p = new Double((Float) o.get(2));
Float p = (Float) o.get(2);
float p = (float) o.get(2);
double p = Double.parseDouble((String) o.get(2));
所有这些都会使websocket崩溃/断开连接
似乎当我尝试访问索引2时,事情变得不稳定
然而
我能行
System.out.println(o.get(2));
很好,可以打印出来
3974.909912109375
# 1 楼答案
您不能直接将其转换为
Double
。相反,尝试使用也就是说,保持数据的一致性,并在处理时将所有内容转换为
Double
可能是一件更好的事情,以避免后续问题