2

i'm using Spring in Android to make a POST call when the error from above pop up. i saw all the answers regards this issue, but in all of them they seem to have arrays or lists in the object they want to transform to JSON. Mine's is very simple:

public class Detection {

private String vehicleId;
private String historyDate;
private double lat;
private double lon;
private double speed;
private double accuracy;
private double bearing;
public Getters/Setters..
public Detection(String vehicleId,String historyDate, double lat,double lon,double speed,double accuracy,double bearing) {
    this.vehicleId=vehicleId;
    this.historyDate=historyDate;
    this.lat=lat;
    this.lon=lon;
    this.speed=speed;
    this.accuracy=accuracy;
    this.bearing=bearing;
}

}

Here is the POST call from an asynchronous task:

class postTask extends AsyncTask<Detection,Void,Void>{
    @Override protected void onPreExecute() {

    }
    @Override
    protected Void doInBackground(Detection... params) {
        try{
            // Set the Content-Type header
            HttpHeaders requestHeaders = new HttpHeaders();
            requestHeaders.setContentType(new MediaType("application","json"));
            HttpEntity<Detection> requestEntity = new HttpEntity<Detection>(params[0], requestHeaders);

            // Create a new RestTemplate instance
            RestTemplate restTemplate = new RestTemplate();

            // Add the Jackson and String message converters
            restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
            restTemplate.getMessageConverters().add(new StringHttpMessageConverter());

            // Make the HTTP POST request, marshaling the request to JSON, and the response to a String
            ResponseEntity<String> responseEntity = restTemplate.exchange("http://wsonline.pladema.net/Monitores.WS/api/androids", HttpMethod.POST, requestEntity, String.class);
            String result = responseEntity.getBody();
        }
        catch (Exception e){
            Log.e("MainActivity", e.getMessage(), e);
        }
        return null;
    }
}

the Debug breaks at the restTemplate.exchange() method and already check the parameter is not null. This is the error message

03-22 12:34:44.635 4533-4654/gps.example.com.myapplicationgps E/MainActivity: Could not read JSON: Can not deserialize instance of java.lang.String out of START_OBJECT token
                                                                           at [Source: buffer(com.android.okhttp.internal.http.HttpConnection$ChunkedSource@259bdd7c).inputStream(); line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
                                                                           at [Source: buffer(com.android.okhttp.internal.http.HttpConnection$ChunkedSource@259bdd7c).inputStream(); line: 1, column: 1]
                                                                          org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance of java.lang.String out of START_OBJECT token
                                                                           at [Source: buffer(com.android.okhttp.internal.http.HttpConnection$ChunkedSource@259bdd7c).inputStream(); line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
                                                                           at [Source: buffer(com.android.okhttp.internal.http.HttpConnection$ChunkedSource@259bdd7c).inputStream(); line: 1, column: 1]
                                                                              at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readInternal(MappingJackson2HttpMessageConverter.java:126)
                                                                              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.exchange(RestTemplate.java:415)
                                                                              at gps.example.com.myapplicationgps.MainActivity$postTask.doInBackground(MainActivity.java:202)
                                                                              at gps.example.com.myapplicationgps.MainActivity$postTask.doInBackground(MainActivity.java:182)
                                                                              at android.os.AsyncTask$2.call(AsyncTask.java:292)
                                                                              at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                              at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                                                                              at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                              at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                              at java.lang.Thread.run(Thread.java:818)
                                                                           Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
                                                                           at [Source: buffer(com.android.okhttp.internal.http.HttpConnection$ChunkedSource@259bdd7c).inputStream(); line: 1, column: 1]
                                                                              at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:691)
                                                                              at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:46)
                                                                              at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:11)
                                                                              at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2993)
                                                                              at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2158)
                                                                              at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readInternal(MappingJackson2HttpMessageConverter.java:123)
                                                                                ... 15 more

Sorry for the bad english

1 Answer 1

1

The response you're receiving is obviously application/json, that's why Jackson kicks in and tries to parse a JSON object. You should specify an Accept header to indicate which media types are acceptable as a response:

requestHeaders.setAccept(new MediaType("text","plain"));

This way the StringHttpMessageConverter should be used by the RestTemplate to convert the response - if the remote server supports serving text/plain for your requested resource.

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry for that. i didn't write it because it says the same over and over. I will edit it now
by the way, it wasn't the default constructor but thanks anyways!
it was my bad, thought i fixed it because i got the messages at the server but the error message is still there (sorry for the long time). Anyway i got an Accept header for json/application so wasn't that. Turns out that the response i get from the server was a complex json that can't fit into String class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.