1
我在Android中使用Spring進行POST調用,當上面的錯誤彈出時。我看到所有的答案都是關於這個問題的,但是在他們看來,他們似乎在他們想要轉換爲JSON的對象中有數組或列表。煤礦的很簡單:無法讀取JSON:無法將START_OBJECT令牌的java.lang.String實例反序列化(非常簡單的對象)
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;
}
}
這裏是一個異步任務的POST電話:
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()方法,並已經檢查參數不爲空。這是錯誤消息
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([email protected]9bdd7c).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([email protected]9bdd7c).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([email protected]9bdd7c).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([email protected]9bdd7c).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([email protected]9bdd7c).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
對不起,您收到的英語不好
對不起。我沒有寫,因爲它一遍又一遍地說。順便說一句,我現在將編輯它 –
,它不是默認的構造函數,但謝謝! –
@martincremona我更新了我的回答 – msparer