我發送數據使用JSON從Java到PHP,使用下面的代碼:的java發送UTF-16數據PHP不工作
String url = "abc.php";
JSONObject json = new JSONObject();
json.put("msg", message); // message: "\ud83d\udc4d \ud83d\udc4e"
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 100000);
HttpPost post = new HttpPost(url);
StringEntity se = new StringEntity("json="+json.toString());
post.addHeader("content-type", "application/x-www-form-urlencoded");
post.setEntity(se);
HttpResponse response;
response = client.execute(post);
String resFromServer = org.apache.http.util.EntityUtils.toString(response.getEntity());
Log.i("resFromServer", resFromServer);
的PHP代碼:
if(isset($_POST["json"])) {
$jsonDecode = json_decode($_POST["json"]);
$msg = $jsonDecode->{"msg"};
echo $msg;
}
但我'得到輸出爲?
而輸出應該
有一些編碼的問題?這怎麼解決?
我認爲你需要使用PHP'mb_convert_encoding()'或'發送UTF-8'從Java,而不是'UTF-16'。在解碼json之前轉換它。 – frz3993
@ frz3993請問你能告訴如何? – user5155835
嘗試'$ input = mb_convert_encoding($ _ POST [「json」],「UTF-8」,「UTF-16」);'。然後'$ jsonDecode = json_decode($ input);'繼續執行你的代碼。 – frz3993