我正在使用JSON从java向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;
}
但我的输出为 ????
而输出应为
是否存在编码问题?如何解决这个问题?
答案 0 :(得分:0)
首先尝试将请求的内容转换为utf8 那些字符串实际上可能不是UTF-16。所以要安全并尝试
if( isset($_POST["json"]) ) {
$string=$_POST['json'];
$jsonDecode = mb_convert_encoding($string, "UTF-8", mb_detect_encoding($string));
$jsonDecode = json_decode($jsonDecode);
$msg = $jsonDecode->{"msg"};
echo $msg;
}