使用utf 8从android发送html post json数组到php并输入数据库?

时间:2019-06-04 17:29:13

标签: php android json utf-8

我之前已经这样做过,但是在我的其他项目中,我没有utf8值,我认为这将它汇总起来,但是不确定
android发送json数组函数:

 URL url = new URL(MainActivity.URL2);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
                conn.setRequestProperty("Accept", "application/json");
                conn.setDoOutput(true);
                conn.setDoInput(true);
                Log.i("JSON", arr.toString());
                if (arr.length() > 0) {
                    DataOutputStream os = new DataOutputStream(conn.getOutputStream());

                    os.writeBytes(arr.toString());

                    os.flush();
                    os.close();

和php端:

$input=file_get_contents("php://input");

$arr = json_decode($input, true);


foreach ($arr as $key => $value) {


    $city=$value["city"];
    $street=$value["street"];
    $type=$value["type"];
    $neighborhood=$value["neb"];
    $floor=$value["floor"];
    $rooms=$value["rooms"];
    $size=$value["size"];
    $price=$value["price"];
    $query="INSERT INTO `d1`"
                         ." (`type`, `city`, `street`, `neighborhood`, `size`, `rooms`, `floor`, `price`) "
                         ."VALUES ('$type', '$city', '$street', '$neighborhood', "
        . "'$size', '$rooms', '$floor', '$price');";
    $mysqli->query($query);



  }

我在utf8的php中尝试了一些功能,但似乎没有帮助
 ini_set('default_charset','utf-8');在php中没有帮助
有人遇到过utf8这个问题吗?
我得到错误:警告:为foreach()提供的参数无效
解码未返回json数组

输入样本:(数组的一部分)

{"price":"1,790,000 ʢ,"floor":"2","street":"ߢ֪֠ Ҩܔ 2","neb":" թ֞蠠鑩֟ ݦڕߢ,"type":"ԙ锢,"size":"100","rooms":"4","city":"鑩֟ ݦڕߢ},{"price":"1,170,000 ʢ,"floor":"4","street":"ԕӠᚑ 22","neb":" ᖔ אѠ ᑕꡐڜߠ Ґ衩Ң","type":"ԙ锢,"size":"117","rooms":"4","city":"Ґ衩Ң"}

保存到:

 $myfile = file_put_contents('testinput2.txt',$input , FILE_APPEND | LOCK_EX);

0 个答案:

没有答案