小编vir*_*l82的帖子

JSON无效的UTF-8中间字节

当(Jackson,本例)JSON引擎尝试解析一些未以UTF-8编码的JSON时,会发生此错误.

如何告诉引擎它应该期望与UTF-8不同的东西,例如UTF-16?

HttpHeaders requestHeaders = createSomeHeader();
RestTemplate restTemplate = new RestTemplate();
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
String url = "someurl"
ResponseEntity<MyObject[]> arrayResponseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, MyObject[].class);
Run Code Online (Sandbox Code Playgroud)

错误日志:

Caused by: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Invalid UTF-8 middle byte 0x20
at [Source: org.apache.http.conn.EofSensorInputStream@44d397b0; line: 92, column: 42]; nested exception is org.codehaus.jackson.JsonParseException: Invalid UTF-8 middle byte 0x20
at [Source: org.apache.http.conn.EofSensorInputStream@44d397b0; line: 92, column: 42]
at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:138)
at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:154)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:74)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:622)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:608)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:449)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:404)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:380)
... …
Run Code Online (Sandbox Code Playgroud)

json jackson

30
推荐指数
3
解决办法
8万
查看次数

Spring Android:使用RestTemplate和https以及cookie

我需要在Android本机应用程序的https连接上使用cookie.我正在使用RestTemplate.

检查其他线程(例如,使用RestTemplate设置安全cookie)我能够在http连接中处理cookie:

restTemplate.setRequestFactory(new YourClientHttpRequestFactory());
Run Code Online (Sandbox Code Playgroud)

哪里 YourClientHttpRequestFactory extends SimpleClientHttpRequestFactory

这适用于http但不适用于https.

另一方面,我能够解决Android信任SSL证书的https问题:

restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(HttpUtils.getNewHttpClient()));
Run Code Online (Sandbox Code Playgroud)

这里描述了HttpUtils:http: //www.makeurownrules.com/secure-rest-web-service-mobile-application-android.html

我的问题是我需要使用ClientHttpRequestFactory的单个实现.所以我有3个选择:

1)找到一种使用SimpleClientHttpRequestFactory处理https的方法

2)找到一种使用HttpComponentsClientHttpRequestFactory处理cookie的方法

3)使用另一种方法

spring android resttemplate

11
推荐指数
1
解决办法
9076
查看次数

标签 统计

android ×1

jackson ×1

json ×1

resttemplate ×1

spring ×1