无法使用 Resttemplate 和 json 从 Android 客户端获取字符编码
好吧,所以我尝试了一切,但在 stackoverflow 上询问...
我正在尝试使用一些 http 参数从 Android 使用 httpclient 和 Resttemplate 到服务器端 Spring 控制器执行 REST 调用。我所有的瑞典字符最终在服务器上作为“\u001A”...
设置httpclient和resttemplate代码:
HttpClient httpClient = new HttpClient();
Credentials defaultcreds = new UsernamePasswordCredentials(msisdn, password);
httpClient.getState().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM), defaultcreds);
//httpClient.getParams().setContentCharset(prefs.());
httpClient.getParams().setCredentialCharset(prefs.getCredentialsEncoding());
CommonsClientHttpRequestFactory requestFactory = new CommonsClientHttpRequestFactory(httpClient);
RestTemplate restTemplate = new RestTemplate(requestFactory);
// Add message converters
List<HttpMessageConverter<?>> mc = restTemplate.getMessageConverters();
MappingJacksonHttpMessageConverter json = new MappingJacksonHttpMessageConverter();
List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
supportedMediaTypes.add(MediaType.APPLICATION_JSON);
json.setSupportedMediaTypes(supportedMediaTypes);
mc.add(json);
restTemplate.setMessageConverters(mc);
return restTemplate;
然后我用我的参数准备一个httpentity:
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("myparam", "" + "å");
HttpEntity<String> entity = new HttpEntity<String>(headers);
我最后进行其余调用:
ResponseEntity<UserStatusTO> response = rest.exchange(url, HttpMethod.GET, entity,
MyResponseClass.class);
在服务器上,我有一个杰克逊反序列化器和我的弹簧控制器方法如下所示:
@RequestMapping(method = RequestMethod.GET, value = "/event/out", headers = "Accept=application/json", produces = {"application/xml;charset=UTF-8", "application/json;charset=UTF-8"})
public
@ResponseBody
UserStatusTO out(Authentication auth, @RequestHeader(value="myparam", required = false) String myparam) {
所有特殊字符最终都为 \u001a !我已经尝试了很多东西,手动重新编码客户端和服务器端的字符串,但没有任何效果。我试过摆弄 httpClient.getParams().setContentCharset(); httpClient.getParams().setUriCharset();
据我所知,没有一个有效。
我没主意了!如果有人有任何意见,我将非常感激。谢谢!
Ok, so ive tried everything but asking at stackoverflow...
I'm trying to perform a REST call with some http params from an Android using httpclient and resttemplate to a server-side Spring controller. All my Swedish chars end up on the server as '\u001A'...
setting up httpclient and resttemplate code:
HttpClient httpClient = new HttpClient();
Credentials defaultcreds = new UsernamePasswordCredentials(msisdn, password);
httpClient.getState().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM), defaultcreds);
//httpClient.getParams().setContentCharset(prefs.());
httpClient.getParams().setCredentialCharset(prefs.getCredentialsEncoding());
CommonsClientHttpRequestFactory requestFactory = new CommonsClientHttpRequestFactory(httpClient);
RestTemplate restTemplate = new RestTemplate(requestFactory);
// Add message converters
List<HttpMessageConverter<?>> mc = restTemplate.getMessageConverters();
MappingJacksonHttpMessageConverter json = new MappingJacksonHttpMessageConverter();
List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
supportedMediaTypes.add(MediaType.APPLICATION_JSON);
json.setSupportedMediaTypes(supportedMediaTypes);
mc.add(json);
restTemplate.setMessageConverters(mc);
return restTemplate;
I then prepare a httpentity with my parameter:
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("myparam", "" + "å");
HttpEntity<String> entity = new HttpEntity<String>(headers);
I finally make the rest call:
ResponseEntity<UserStatusTO> response = rest.exchange(url, HttpMethod.GET, entity,
MyResponseClass.class);
On the server, i have a jackson deserializer and my spring controller method looks like:
@RequestMapping(method = RequestMethod.GET, value = "/event/out", headers = "Accept=application/json", produces = {"application/xml;charset=UTF-8", "application/json;charset=UTF-8"})
public
@ResponseBody
UserStatusTO out(Authentication auth, @RequestHeader(value="myparam", required = false) String myparam) {
All the special chars end up as \u001a ! I've tried tons of stuff, re-encoding the strings manually client AND server side, none worked. I've tried fiddling with the
httpClient.getParams().setContentCharset();
httpClient.getParams().setUriCharset();
None worked as far as i could tell.
I'm out of ideas! If anybody has any input, i'd be much obliged. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了类似的问题,我通过手动设置正确的内容类型和字符集而不是使用 MediaType.APPLICATION_JSON 来修复它:
I had a similar issue and I fixed it by setting the proper content-type AND character set manually instead of using MediaType.APPLICATION_JSON:
这听起来像是服务器端的一个问题,因为 Apache HTTP 客户端很乐意在标头中输出 Latin-1 字符,您可以使用这个简单的测试进行测试:
SessionOutputBufferImpl 是使用 AbstractSessionOutputBuffer 的一个小技巧
您可能应该专注于您的服务器后端- 你用什么?如果它是“固定的” - 您应该考虑尝试根据 RFC 2407 格式化您的标头 -一些服务器支持,另一些则惨败。
This sounds like an issue on the server side since the Apache HTTP client will gladly output Latin-1 characters in headers, as you can test with this simple test:
SessionOutputBufferImpl is a little hack to use the AbstractSessionOutputBuffer
You should probably concentrate on your server backend - what are you using? If it's "fixed" - you should consider trying to format your header according to RFC 2407 - some servers support, others fail miserably.