这是我的示例代码,但是我使用了HttpProtocolParams.setContentCharset(params,“utf-8”);即时通讯是我的代码,但是当我发送utf-8数据时,我只会收到“?????”在服务器端(PHP代码)!问题是什么?
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "utf-8");
HttpClient httpclient = new DefaultHttpClient(params);
httpclient.getParams().setParameter("http.protocol.content-charset", "UTF-8");
HttpPost httppost = new HttpPost("URL_TO_SERVER");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("uname", name.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("uemail", email.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("udesc", body.getText().toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse hresponse = httpclient.execute(httppost);
String response = new Scanner(hresponse.getEntity().getContent(), "UTF-8").useDelimiter("\\A").next();
hresponse.getEntity().consumeContent();
httpclient.getConnectionManager().shutdown();
Log.d("WX", response.toString());
Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
最佳答案
也许您应该对实体使用UTF-8:
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
关于java - 使用HttpClient将带有post方法的utf8内容发送到android中的服务器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8964291/