如何在UTF-8中通过http post发送字符串

时间:2013-03-10 12:04:46

标签: android

我尝试发送字符串“Приветмир!”

String link = POST_URL;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(link);
String xml ="Привет мир";
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("file", xml));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);

并通过php脚本保存:

if(!empty($_REQUEST['file'])){
$fp = fopen("C:\\windows\\temp\\1.xml", "w");
$mytext =$_POST["file"];
$test = fwrite($fp, $mytext); 
fclose($fp); 

但是我得到了?????? ?????在我的网络服务器上,我尝试使用utf编码重新打开文件,但它没有帮助。我该如何解决呢。

2 个答案:

答案 0 :(得分:15)

StringEntity的UTF-8字符集。这些行将起到作用:

 httpPost.setEntity(new StringEntity(body, HTTP.UTF_8));

答案 1 :(得分:4)

这对我有用:

HttpPost httpPost = new HttpPost("http://someurl.com");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair, HTTP.UTF_8));