Php 分析数据json时出错。。。
我正在开发一个android应用程序,并试图允许用户通过php/mysql Web服务登录。 然而,该应用程序在输入电子邮件和密码后崩溃。检查logcat,我看到以下内容 JSON显示为--nnn{“success”:“1”,“uid”:“111”,“name”:“test”,“email”:“test”}n 然后错误——解析数据org.json.JSONException时出错:java.lang.String类型的值nnn无法转换为JSONObject 这个“nnn”是从哪里来的?我在我的代码中看不到这一点(可能是打字错误) 这是php-->Php 分析数据json时出错。。。,php,android,json,Php,Android,Json,我正在开发一个android应用程序,并试图允许用户通过php/mysql Web服务登录。 然而,该应用程序在输入电子邮件和密码后崩溃。检查logcat,我看到以下内容 JSON显示为--nnn{“success”:“1”,“uid”:“111”,“name”:“test”,“email”:“test”}n 然后错误——解析数据org.json.JSONException时出错:java.lang.String类型的值nnn无法转换为JSONObject 这个“nnn”是从哪里来的?我在我的代
非常感谢。原来是我的JSONParser中的一个输入错误 而((line=reader.readLine())!=null){ sb.附加(第+行“n”) 已更改为\n,错误消失 谢谢
// get tag
if (isset($_POST['tag']) && $_POST['tag'] != '') {
$tag = $_POST['tag'];
//json response array
$response = array();
// check for tag type
if ($tag == 'login') {
$email = $_POST['email'];
$password = $_POST['password'];
//check user exists
$qry = "SELECT * FROM users WHERE email = ? AND password = ? ";
$stmt = $mysqli->prepare($qry);
$stmt->bind_param('ss', $email, $password);
if($stmt->execute()){
$result = $stmt->get_result();
$user = $result->fetch_assoc();
// if user exists, set json values
$response["success"] = "1";
$response["uid"] = $user["unique_id"];
$response["name"] = $user["name"];
$response["email"] = $user["email"];
echo json_encode($response);
} else {
// user not found
$response["error"] = 1;
$response["error_msg"] = "Incorrect email or password!";
echo json_encode($response);
exit;
}
// else register new user
} else if ($tag == 'register') {
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
// first check if user already exists
$query = "SELECT email FROM users WHERE email=?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param('s',$email);
$stmt->execute();
/* store result */
$stmt->store_result();
if ($stmt->num_rows > 0){
//if result greater than 0 , email exists
$response["error"] = 2;
$response["error_msg"] = "Email address already registered.";
echo json_encode($response);
exit;
} else {
// create user
$qry = "INSERT INTO users (name, email, password) VALUES(?, ?, ?)";
$stmt = $mysqli->prepare($qry);
$stmt->bind_param('sss', $name, $email, $password);
if($stmt->execute())
{
$uid = mysql_insert_id();
// user stored successfully
$response["success"] = 1;
$response["uid"] = $uid;
$response["name"] = $name;
$response["email"] = $email;
echo json_encode($response);
} else {
// user failed to store
$response["error"] = 1;
$response["error_msg"] = "Error with Registartion";
echo json_encode($response);
}
}
} else {
echo "Invalid Request";
}
} else {
echo "Access Denied";
}
?>