好了,我用盡一切,但在詢問計算器...無法獲得字符編碼來自Android客戶端工作,resttemplate和JSON
我試圖用從某些HTTP PARAMS執行REST調用Android使用httpclient和resttemplate到服務器端的Spring控制器。我所有的瑞典字符落得服務器 '\ u001A' 上......
設置的HttpClient和resttemplate代碼:
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("myparam", "" + "å");
HttpEntity<String> entity = new HttpEntity<String>(headers);
:
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
我終於做出REST調用:
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()。設置內容集合哈希函數()來調用 ; httpClient.getParams()。setUriCharset();
沒有工作,據我所知。
我出來的想法!如果有人有任何意見,我會很感激。謝謝!
除了「myparam」頭,你在哪裏在惡補å,ä:s和ö:s? – Jens 2012-02-02 11:22:28
好吧,只有一個參數有瑞典字符。我縮短了我的例子,使它更容易閱讀,但在我的應用程序中有一個字段,用戶可以輸入筆記,我試圖發送到服務器...但是,我有「å」的地方是我在我的代碼放在文本域的文本 – Mathias 2012-02-02 11:59:20