0

我正在开发一个 android 应用程序并尝试允许用户通过 php/mysql 网络服务登录。然而,该应用程序在输入电子邮件和密码后崩溃。检查logcat我看到以下...

JSON 显示为 -- nnn{"success":"1","uid":"111","name":"test","email":"test"}n

然后报错——Error parsing data org.json.JSONException: Value nnn of type java.lang.String cannot be convert to JSONObject

这个“nnn”来自什么或哪里?我无法在我的代码中发现这一点(认为这可能是一个错字)

这是 php -->

// 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";
}
?>

非常感谢。

4

1 回答 1

1

原来是我的 JSONParser 中的一个错字

while ((line = reader.readLine()) != null) { sb.append(line + "n");

更改为 \n 并且错误消失了。

谢谢。

于 2013-09-18T12:45:12.910 回答