androidjsonjacksonrobospiceape

Jackson - cannot seserialize array, UnknownLengthHttpInputStream


I am writing an implementation of APE protocol for chat on Android. When I send request to connect the answer is:

[
    {
        "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"
                }
            }
        }
    }
]

My POJO is:

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;
    }
}

When parsing JSON I have exception:

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)

So the exception is: UnknownLengthHttpInputStream. Maybe this could be the problem: the response content type from server is text/html (I cannot change it) so I added this type to Jackson converter supported media types using - method. Could it be the reason of the error?


Solution

  • Actually in my case link from the comments wasn't the answer. I could not use ObjectMapper because that was done automatically for my bo other library. The solution was to make the POJO extend ArrayList.