当我弹出上面的错误时,我在Android中使用Spring进行POST调用。我看到所有的答案都关注这个问题,但是在所有这些问题中,他们似乎都在要转换为JSON的对象中有数组或列表。我的很简单:
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;
}
}
Debug在breakTemplate.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
抱歉英文不好
答案 0 :(得分:0)
您收到的回复显然是application/json
,这就是Jackson
启动并尝试解析JSON对象的原因。您应指定Accept header以指示哪些媒体类型可接受为响应:
requestHeaders.setAccept(new MediaType("text","plain"));
这样,如果远程服务器支持为您请求的资源提供服务StringHttpMessageConverter
,RestTemplate
应使用text/plain
来转换响应。