Tuesday, 15 April 2014

java - JSON encoding returning NULL -



java - JSON encoding returning NULL -

im working on app users needs login to. coded in java, login works without problems. however, want receive "message" json , display it. said, code works, if json string "message" dosent contain special characters æ, ø, å. i've tried anything, also, have file gets info db , shows in listview, used exact same code, , works. json in login.php returns null, replaces whole string , replaces null.

my login.php (returning null)

<?php header('content-type:text/json;charset=utf-8'); $uname = $_post["txtuname"]; $pass = $_post["txtpass"]; $con=mysqli_connect("localhost","user","pass","db");// check connection if (mysqli_connect_errno()) { $response["success"] = 0; $response["message"] = "database error!"; die(json_encode($response)); echo "failed connect mysql: " . mysqli_connect_error(); } //mysql_query("set names 'utf8'", $con); mysql_query("set names 'utf8'"); $result = mysqli_query($con,"select * users email='$uname' , encrypted_password='$pass'"); while($row = mysqli_fetch_array($result)) { $response["success"] = 1; $response["message"] = $row['firstname'] . " " . $row['lastname']; die(json_encode($response)); //header("location: home.php"); } mysqli_close($con); ?>

my getdata.php (works)

<?php include "db_config.php"; $cat = $_get['app_category']; if($cat == '99'){ mysql_query("set names 'utf8'"); $sql=mysql_query("select * `test_fc`"); while($row=mysql_fetch_assoc($sql)) $output[]=$row; print ('{ "app_item":'); print(json_encode($output)); print ('}'); mysql_close(); } else { mysql_query("set names 'utf8'"); $sql=mysql_query("select * `test_fc` app_category={$cat}"); while($row=mysql_fetch_assoc($sql)) $output[]=$row; print ('{ "app_item":'); print(json_encode($output)); print ('}'); mysql_close(); } ?>

my java parser can read getdata.php without problem, special characters.

i've been stuck on past couple of days. help much appreciated!

edit: getting {"message":null,"success":}

the problem line:

mysql_query("set names 'utf8'");

you using mysql_* function while rest - , database connection - using mysqli_*. lead warning in php gets echoed out , invalidates json.

you should utilize either mysqli (preferred) or mysql (deprecated) not mix them.

java php android mysql json

No comments:

Post a Comment