Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
451 views
in Technique[技术] by (71.8m points)

android - how to send string by http post in UTF-8

I try to send string "Привет мир!"

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

And save it by php script:

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

But I get ?????? ????? on my web server, I try reopen file with utf encoding, but it doesn't help. How can I resolve it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The StringEntity's charset to UTF-8. These lines will do the trick:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...