当前位置: 首页 > 知识库问答 >
问题:

无法读取JSON:无法将START_OBJECT令牌(非常简单的对象)中的java.lang.String实例反序列化

倪风史
2023-03-14
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;
}

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

调试在restTemplate.exchange()方法中断,并且已经检查参数是否为null。这是错误消息

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

为糟糕的英语感到抱歉

共有1个答案

养星汉
2023-03-14

您收到的响应显然是application/JSON,这就是为什么Jackson开始尝试解析JSON对象。您应该指定一个Accept标头,以指示哪些媒体类型可以作为响应接受:

requestheaders.setaccept(新媒体类型(“text”,“plain”));

这样,RESTTemplate应该使用StringhtTPMessageConverter来转换响应-如果远程服务器支持为请求的资源提供text/plain

 类似资料: