这是我的示例代码,但是我使用了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
}
Run Code Online (Sandbox Code Playgroud)
小智 23
也许你应该使用UTF-8作为实体:
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14752 次 |
最近记录: |