我看到這篇文章How to send unicode characters in an HttpPost on Android,但我通常以這種方式在AsyncTask類中請求。我的日誌也在urlParameters中打印本地語言,但服務器在完美時返回沒有結果對於英語字符串:如何從http發送請求中的Unicode字符asyncTask
@Override
protected String doInBackground(String... URLs) {
StringBuffer response = new StringBuffer();
try {
URL obj = new URL(URLs[0]);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// add request header
con.setRequestMethod("POST");
if (URLs[0].equals(URLHelper.get_preleases)) {
urlCall = 1;
} else
urlCall = 2;
// String urlParameters = "longitude=" + longitude + "&latitude="+latitude;
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
if (responseCode == 200) {
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response.toString();
}
有沒有一種方法來設置字符集UTF-8請求參數編碼這種方式?
[how-to-send-unicode-characters-in-an-httppost-on-android](http://stackoverflow.com/questions/5084462/how-to-send-unicode-characters-in-an -httppost-on-android) – SilentKiller 2014-09-02 04:44:05
@SilentKiller多數民衆贊成在他發佈的問題:) – KOTIOS 2014-09-02 04:48:17
@DIVA請檢查此行是否在上面的代碼可用'StringEntity stringEntity = new StringEntity(msg,「UTF-8」);'他不在任何地方提供charSet。 – SilentKiller 2014-09-02 04:49:37