1

我正在为 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 转换器支持的媒体类型。这可能是错误的原因吗?

4

2 回答 2

1

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

于 2014-04-01T07:45:23.103 回答
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的反应来匹配它。

于 2014-03-31T12:20:18.813 回答