我正在尝试将 Spring for Android 与 Jackson 2 一起使用,从休息呼叫中创建 POJO。
// Set the Accept header
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAccept(Collections.singletonList(new MediaType("application","vnd.livescore_app.api.v1+json")));
requestHeaders.setContentType(new MediaType("application","json;charset=utf-8"));
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
// Create a new RestTemplate instance
RestTemplate restTemplate = new RestTemplate();
// Add the Jackson message converter
restTemplate.getMessageConverters().add( new MappingJackson2HttpMessageConverter());
// Make the HTTP GET request, marshaling the response from JSON to an array of Events
ResponseEntity<AppVersionObject> responseEntity = restTemplate.exchange("https://example.com", HttpMethod.GET, requestEntity, AppVersionObject.class);
AppVersionObject object = responseEntity.getBody();
崩溃并出现以下日志跟踪
05-13 10:55:42.656: E/AndroidRuntime(19401): FATAL EXCEPTION: Thread-1470
05-13 10:55:42.656: E/AndroidRuntime(19401): java.lang.IllegalArgumentException:Invalid token character ';' in token "json;charset=utf-8"
05-13 10:55:42.656: E/AndroidRuntime(19401): at org.springframework.http.MediaType.checkToken(MediaType.java:377)
05-13 10:55:42.656: E/AndroidRuntime(19401): at org.springframework.http.MediaType.<init>(MediaType.java:351)
05-13 10:55:42.656: E/AndroidRuntime(19401): at org.springframework.http.MediaType.<init>(MediaType.java:303)
05-13 10:55:42.656: E/AndroidRuntime(19401): at com.madinsweden.livescoretennis.service.CheckAppVersionTask.run(CheckAppVersionTask.java:41)
05-13 10:55:42.656: E/AndroidRuntime(19401): at java.lang.Thread.run(Thread.java:856)
我当然可以将内容类型更改为更标准化application/json
,但随后出现以下错误。
05-13 11:03:07.054: E/AndroidRuntime(20010): org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [com.madinsweden.livescoretennis.backend.AppVersionObject] and content type [application/vnd.livescore_app.api.v1+json;charset=utf-8]
05-13 11:03:07.054: E/AndroidRuntime(20010): at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:79)
05-13 11:03:07.054: E/AndroidRuntime(20010): at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:655)
05-13 11:03:07.054: E/AndroidRuntime(20010): at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(Rest Template.java:641)
05-13 11:03:07.054: E/AndroidRuntime(20010): at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:484)
05-13 11:03:07.054: E/AndroidRuntime(20010): at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:439)
05-13 11:03:07.054: E/AndroidRuntime(20010): at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:415)
05-13 11:03:07.054: E/AndroidRuntime(20010): at com.madinsweden.livescoretennis.service.CheckAppVersionTask.run(CheckAppVersionTask.java:51)
05-13 11:03:07.054: E/AndroidRuntime(20010): at java.lang.Thread.run(Thread.java:856)