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");