android http post請求,設定utf-8編碼,服務端還是出現中文亂碼 解決
阿新 • • 發佈:2019-02-04
HttpClient httpClient = new DefaultHttpClient(); HttpPost post = new HttpPost(url);List<NameValuePair> params = new ArrayList<>(); params.add(new BasicNameValuePair("info", "測試")); try { UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(params, HTTP.UTF_8);post.setEntity(uefEntity); } catch(UnsupportedEncodingException e) { Log.e("test", "test"); } HttpResponse response = null; try { response = httpClient.execute(post); } catch (Exception e) { …… } if (response.getStatusLine().getStatusCode() == 200) { …… }
服務端接收到後顯示為亂碼。
解決方法:
HttpPost post = new HttpPost(url); post.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");