mrniko / netty-socketio

Socket.IO server implemented on Java. Realtime java framework
Apache License 2.0
6.77k stars 1.65k forks source link

EventListener Can not parse JSON string to Java Object #258

Open zhuoran opened 9 years ago

zhuoran commented 9 years ago

In my case I used JacksonJsonSupport to parse json object

config.setJsonSupport(new JacksonJsonSupport());
config.getJsonSupport().addEventMapping("", "message", ChatEvent.class);
            ....
server.addEventListener("message", ChatEvent.class,chatEventListener);

when I received user data from ChatEventListener and I always get a error!

This problem occurs only when the Android client, JavaScript client has no problem

Android Client Code Both of these two methods are parse failed

ChatEvent data = new ChatEvent();
mSocket.emit("message", JSON.toJSONString(data));
ChatEvent data = new ChatEvent();
mSocket.emit("message", data);

Any Idea for this?

Thx

···java [com.corundumstudio.socketio.JsonSupportWrapper]-[ERROR] Can't read value: ["message","{\"action\":1,\"content\":\"ffc\",\"date\":1437811543560,\"from\":1,\"nickName\":\"test\",\"roomId\":\"1001\",\"uid\":\"AYGQNVGEQOZ95POR\"}"] for type: class com.corundumstudio.socketio.protocol.Event com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class com.xxx.chat.protocol.ChatEvent] from String value ('{"action":1,"content":"ffc","date":1437811543560,"from":1,"nickName":"test","roomId":"1001","uid":"AYGQNVGEQOZ95POR"}'); no single-String constructor/factory method at [Source: io.netty.buffer.ByteBufInputStream@21a1f581; line: 1, column: 11] at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148) at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:757) at com.fasterxml.jackson.databind.deser.ValueInstantiator._createFromStringFallbacks(ValueInstantiator.java:277) at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:289) at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromString(BeanDeserializerBase.java:1133) at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:135) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:126) at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:3023) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1637) at com.corundumstudio.socketio.protocol.JacksonJsonSupport$EventDeserializer.deserialize(JacksonJsonSupport.java:194) at com.corundumstudio.socketio.protocol.JacksonJsonSupport$EventDeserializer.deserialize(JacksonJsonSupport.java:155) at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3051) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2192) at com.corundumstudio.socketio.protocol.JacksonJsonSupport.readValue(JacksonJsonSupport.java:337) at com.corundumstudio.socketio.JsonSupportWrapper.readValue(JsonSupportWrapper.java:52) at com.corundumstudio.socketio.protocol.PacketDecoder.parseBody(PacketDecoder.java:303) at com.corundumstudio.socketio.protocol.PacketDecoder.decode(PacketDecoder.java:184) at com.corundumstudio.socketio.protocol.PacketDecoder.decodePackets(PacketDecoder.java:150) at com.corundumstudio.socketio.handler.InPacketHandler.channelRead0(InPacketHandler.java:65) at com.corundumstudio.socketio.handler.InPacketHandler.channelRead0(InPacketHandler.java:36) at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:187) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:846) at com.corundumstudio.socketio.transport.WebSocketTransport.channelRead(WebSocketTransport.java:94) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at com.corundumstudio.socketio.transport.PollingTransport.channelRead(PollingTransport.java:109) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at com.corundumstudio.socketio.handler.AuthorizeHandler.channelRead(AuthorizeHandler.java:115) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:108) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:182) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:846) at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:130) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511) at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354) at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116) at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137) at java.lang.Thread.run(Thread.java:745) 2015-07-25 16:03:05.042 [com.corundumstudio.socketio.handler.InPacketHandler]-[ERROR] Error during data processing. Client sessionId: d2a0be61-0ab0-437f-9c1e-2b325ae316d2, data: java.lang.NullPointerException at com.corundumstudio.socketio.protocol.PacketDecoder.parseBody(PacketDecoder.java:304) at com.corundumstudio.socketio.protocol.PacketDecoder.decode(PacketDecoder.java:184) at com.corundumstudio.socketio.protocol.PacketDecoder.decodePackets(PacketDecoder.java:150) at com.corundumstudio.socketio.handler.InPacketHandler.channelRead0(InPacketHandler.java:65) at com.corundumstudio.socketio.handler.InPacketHandler.channelRead0(InPacketHandler.java:36) at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:187) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:846) at com.corundumstudio.socketio.transport.WebSocketTransport.channelRead(WebSocketTransport.java:94) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at com.corundumstudio.socketio.transport.PollingTransport.channelRead(PollingTransport.java:109) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at com.corundumstudio.socketio.handler.AuthorizeHandler.channelRead(AuthorizeHandler.java:115) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:108) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:182) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:846) at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:130) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511) at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354) at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116) at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137) at java.lang.Thread.run(Thread.java:745)

nebyan commented 7 years ago

Hi @zhuoran did you find any solution for this problem

atlas1119 commented 7 years ago

Hi @zhuoran, I have the same problem, do you fixed it??

jiangbaojun commented 6 years ago

Android client use json parser is "org.json",when emit message,you should like this: ChatEvent data = new ChatEvent(); mSocket.emit("message", new JSONObject(data));

eshiett1995 commented 5 years ago

@jiangbaojun is right, i also added a correction to his tho

mSocket.emit("message", new JSONObject(new Gson().toJson(data)));

try and convert the object you are trying to send to a string using GSON library

jingzhongyue commented 5 years ago

my Solution: the bean add constructor by createFromString like this:

`class TestBean {

private String x;
private String y;
private String z;

public TestBean() {}

public TestBean(String beanStr) {
    // StdValueInstantiator.createFromString
    // _fromStringCreator
    JSONObject parseObject = JSON.parseObject(beanStr);
    setX(parseObject.getString("x"));
    setY(parseObject.getString("y"));
    setZ(parseObject.getString("z"));
}`

you can grab the packet By wrieshark Look at the differences between JS and Android package

the package Sketch Map:

image

JS-Client package: image

java-client package: image

Dmunch04 commented 4 years ago

@jingzhongyue Hey. I'm experiencing the same problem as he does. Except that it's the JS client that doesn't work for me. Would you mind explaining that solution, as I'm not quite sure what you mean?