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
为糟糕的英语感到抱歉
您收到的响应显然是application/JSON
,这就是为什么Jackson
开始尝试解析JSON对象。您应该指定一个Accept标头,以指示哪些媒体类型可以作为响应接受:
requestheaders.setaccept(新媒体类型(“text”,“plain”));
这样,RESTTemplate
应该使用StringhtTPMessageConverter
来转换响应-如果远程服务器支持为请求的资源提供text/plain
。
我尝试声明为String或List,但没有成功。知道吗? }
例外情况: 套接字处理程序 我相信当JSON被解析为WorkstationRequest对象时会出现异常,原因是下面的项。这是套接字处理程序: 我不知道如何开始调试它。堆栈跟踪从未触及我的应用程序。我正在使用部署我的.jar,并使用执行它
我有rest url,可以显示所有国家--http://api.geonames.org/countryInfojson?username=volodial。 如何告诉将数组的每个元素转换为对象?
我第一次遇到ObjectMapper与string.class一起使用,我尝试将json作为string提供,得到了JSONMappingException。你能帮我理解一下为什么吗? 答复: 线程“Main”Com.FasterXml.Jackson.DataBind.JsonMappingException异常:无法反序列化位于[Source:{“Response”:“Success”}处的S
问题内容: 我有可以访问所有国家/地区的休息网址-http://api.geonames.org/countryInfoJSON ?username=volodiaL。 我从春季3开始使用RestTemplate将返回的json解析为java对象: 当我运行此代码时,出现异常: 最后是我的国家/地区课程: 问题是返回的json包含根元素“ geonames”,其中包含国家元素数组,如下所示: 如何
如下所示 但是在执行它时,我遇到了以下错误,当它是JSON中的JSON时,我很小心地创建了一个新类,就像在usagenlu中一样