Java 投射导致 websocket 崩溃
Java Casting crashes 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?
}
这是怎么回事??
为什么我不能将其转换为双倍数?
我也尝试过 Float,但没有成功。
有什么想法吗??
伊娃也试过...
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));
所有这些 crash/disconnect websocket。
似乎当我尝试访问索引 2 时,事情变得不稳定。
然而....
我可以
System.out.println(o.get(2));
很好,它会打印
3974.909912109375
您不能将其直接转换为 Double
。而是尝试使用
Double p = Double.parseDouble(o.get(2));
话虽这么说,但保持数据一致并将所有内容都转换为 Double
可能会更好,这样可以避免后续出现问题。
String text = "1,2,3974.909912109375,27500,1639207185";
String[] inputs = text.split(",");
List<Double> doubles = Arrays.stream(inputs)
.map(Double::parseDouble)
.collect(Collectors.toList());
所以我有一个奇怪的问题。 我正在连接到 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?
}
这是怎么回事?? 为什么我不能将其转换为双倍数?
我也尝试过 Float,但没有成功。
有什么想法吗??
伊娃也试过...
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));
所有这些 crash/disconnect websocket。
似乎当我尝试访问索引 2 时,事情变得不稳定。
然而....
我可以
System.out.println(o.get(2));
很好,它会打印
3974.909912109375
您不能将其直接转换为 Double
。而是尝试使用
Double p = Double.parseDouble(o.get(2));
话虽这么说,但保持数据一致并将所有内容都转换为 Double
可能会更好,这样可以避免后续出现问题。
String text = "1,2,3974.909912109375,27500,1639207185";
String[] inputs = text.split(",");
List<Double> doubles = Arrays.stream(inputs)
.map(Double::parseDouble)
.collect(Collectors.toList());