2012-02-02 57 views
0

好了,我用盡一切,但在詢問計算器...無法獲得字符編碼來自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();

沒有工作,據我所知。

我出來的想法!如果有人有任何意見,我會很感激。謝謝!

+0

除了「myparam」頭,你在哪裏在惡補å,ä:s和ö:s? – Jens 2012-02-02 11:22:28

+0

好吧,只有一個參數有瑞典字符。我縮短了我的例子,使它更容易閱讀,但在我的應用程序中有一個字段,用戶可以輸入筆記,我試圖發送到服務器...但是,我有「å」的地方是我在我的代碼放在文本域的文本 – Mathias 2012-02-02 11:59:20

回答

1

這聽起來像是上,因爲Apache的HTTP客戶端服務器端的問題將在首部欣然輸出Latin-1字符,你可以用這個簡單的測試進行測試:

HttpGet get = new HttpGet("http://www.riksdagen.se"); 
get.addHeader("SwedishHeader", "Mona Sahlin är ett nötdjur"); 

ByteArrayOutputStream out = new ByteArrayOutputStream(); 
SessionOutputBufferImpl buffer = new SessionOutputBufferImpl(out, get.getParams()); 
HttpRequestWriter writer = new HttpRequestWriter(buffer, BasicLineFormatter.DEFAULT, get.getParams()); 

writer.write(get); 
buffer.flush(); 
System.out.println(out.toString("ISO-8859-1")); 

SessionOutputBufferImpl有點劈使用AbstractSessionOutputBuffer

class SessionOutputBufferImpl extends AbstractSessionOutputBuffer { 
    SessionOutputBufferImpl(OutputStream out, HttpParams params) { 
     init(out, 1024, params); 
    } 
} 

你應該專注於你的服務器後端 - 你在用什麼?如果它是「固定的」 - 你應該考慮嘗試根據RFC 2407格式化你的頭部 - 一些服務器支持,其他服務器失敗。

+0

我很抱歉,但我可以確認它不是服務器。我花了一天更新我的IOS應用程序具有相同的功能和文本到達罰款,編碼正確,並在數據庫中的所有字符看起來不錯,他們最終... – Mathias 2012-02-03 00:54:02

+0

你wiresharked你的服務器輸入和檢查編碼差異? – Jens 2012-02-03 07:54:54

4

我也有類似的問題,我通過設置適當的內容類型和字符手動設置,而不是使用MediaType.APPLICATION_JSON固定它:

HttpHeaders headers = new HttpHeaders(); 
Charset utf8 = Charset.forName("UTF-8"); 
MediaType mediaType = new MediaType("application", "json", utf8); 
headers.setContentType(mediaType); 
相關問題