杰克逊 - 无法串行化数组,UnknownLengthHttpInputStream

时间:2014-03-31 12:10:21

标签: android json jackson robospice ape

我正在为Android上的聊天编写APE协议的实现。当我发送连接请求的答案是:

[
    {
        "time": "1396265282",
        "raw": "LOGIN",
        "data": {
            "sessid": "51da81ceb3acd4655f95d00c29d058a0"
        }
    },
    {
        "time": "1396265282",
        "raw": "IDENT",
        "data": {
            "user": {
                "casttype": "uni",
                "pubid": "fd1e5426d381e5c9ac4e3b0cb32622d7",
                "properties": {
                    "channel": "chat",
                    "uid": "undefined",
                    "type": "0",
                    "name": "test1426"
                }
            }
        }
    }
]

我的POJO是:

public class ConnectResponse {
    public BasicInfo[] response;

    public static class BasicInfo {
        public String time;
        public String raw;
        public Data data;
    }


    public static class Data {
        public String sessionid;
        public User user;
    }

    public static class User {
        public String casttype;
        public String pubid;
        public Properties properties;
    }

    public static class Properties{
        public String channel;
        public String uid;
        public int type;
        public String name;
    }
}

解析JSON时我有异常:

03-31 13:37:09.258  16829-17365/com.miquido.vheda_android E//DefaultRequestRunner.java:138﹕ 13:37:09.257 Thread-38761 An exception occurred during request network execution :Could not read JSON: Can not deserialize instance of com.miquido.vheda_android.chat.response.ConnectResponse out of START_ARRAY token
            at [Source: libcore.net.http.UnknownLengthHttpInputStream@41fa3030; line: 1, column: 1]; nested exception is org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of com.miquido.vheda_android.chat.response.ConnectResponse out of START_ARRAY token
            at [Source: libcore.net.http.UnknownLengthHttpInputStream@41fa3030; line: 1, column: 1]
    org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance of com.miquido.vheda_android.chat.response.ConnectResponse out of START_ARRAY token
            at [Source: libcore.net.http.UnknownLengthHttpInputStream@41fa3030; line: 1, column: 1]; nested exception is org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of com.miquido.vheda_android.chat.response.ConnectResponse out of START_ARRAY token
            at [Source: libcore.net.http.UnknownLengthHttpInputStream@41fa3030; line: 1, column: 1]
            at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:125)
            at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:147)
            at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:76)
            at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:655)
            at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:641)
            at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:484)
            at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:439)
            at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:340)
            at com.miquido.vheda_android.chat.request.ConnectRequest.loadDataFromNetwork(ConnectRequest.java:45)
            at com.miquido.vheda_android.chat.request.ConnectRequest.loadDataFromNetwork(ConnectRequest.java:23)
            at com.octo.android.robospice.request.CachedSpiceRequest.loadDataFromNetwork(CachedSpiceRequest.java:45)
            at com.octo.android.robospice.request.DefaultRequestRunner.processRequest(DefaultRequestRunner.java:134)
            at com.octo.android.robospice.request.DefaultRequestRunner$1.run(DefaultRequestRunner.java:201)
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)
     Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of com.miquido.vheda_android.chat.response.ConnectResponse out of START_ARRAY token
            at [Source: libcore.net.http.UnknownLengthHttpInputStream@41fa3030; line: 1, column: 1]
            at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:219)
            at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:212)
            at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromArray(BeanDeserializer.java:869)
            at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:597)
            at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2723)
            at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1914)
            at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:122)
            at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:147)
            at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:76)
            at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:655)
            at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:641)
            at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:484)
            at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:439)
            at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:340)
            at com.miquido.vheda_android.chat.request.ConnectRequest.loadDataFromNetwork(ConnectRequest.java:45)
            at com.miquido.vheda_android.chat.request.ConnectRequest.loadDataFromNetwork(ConnectRequest.java:23)
            at com.octo.android.robospice.request.CachedSpiceRequest.loadDataFromNetwork(CachedSpiceRequest.java:45)
            at com.octo.android.robospice.request.DefaultRequestRunner.processRequest(DefaultRequestRunner.java:134)
            at com.octo.android.robospice.request.DefaultRequestRunner$1.run(DefaultRequestRunner.java:201)
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)

所以异常是:UnknownLengthHttpInputStream。 也许这可能是问题:来自服务器的响应内容类型是text / html(我无法更改)所以我使用 - method将此类型添加到Jackson转换器支持的媒体类型。 这可能是错误的原因吗?

2 个答案:

答案 0 :(得分:1)

实际上在我的情况下,评论中的链接并不是答案。我无法使用ObjectMapper,因为这是我的bo其他库自动完成的。 解决方案是使POJO扩展ArrayList。

答案 1 :(得分:0)

根据您的POJO,杰克逊会期望JSON这样:

{
    "response": [
        {
            "time": "1396265282",
            "raw": "LOGIN",
            "data": {
                "sessid": "51da81ceb3acd4655f95d00c29d058a0"
            }
        },
        {
            "time": "1396265282",
            "raw": "IDENT",
            "data": {
                "user": {
                    "casttype": "uni",
                    "pubid": "fd1e5426d381e5c9ac4e3b0cb32622d7",
                    "properties": {
                        "channel": "chat",
                        "uid": "undefined",
                        "type": "0",
                        "name": "test1426"
                    }
                }
            }
        }
    ]
}

因此,您必须更改POJO或更改您的回复才能与之匹配。